1616#include " nanobind/stl/string.h" // for type conversion
1717
1818#include < cstddef>
19+ #include < cstdint>
1920#include < exception>
20- #include < iostream>
2121#include < string>
2222
2323#include " llvm/ADT/STLExtras.h"
2626#include " llvm/ADT/StringRef.h"
2727#include " llvm/ADT/TypeSwitch.h"
2828#include " llvm/Support/Casting.h"
29- #include " llvm/Support/DebugLog.h"
3029#include " llvm/Support/raw_ostream.h"
3130#include " mlir/IR/Attributes.h"
3231#include " mlir/IR/BuiltinAttributes.h"
32+ #include " mlir/IR/BuiltinTypeInterfaces.h"
3333#include " mlir/IR/TypeRange.h"
3434#include " mlir/IR/Types.h"
35+ #include " mlir/Support/LLVM.h"
3536
3637#include " Quantum/IR/QuantumInterfaces.h"
3738#include " Quantum/IR/QuantumOps.h"
@@ -44,23 +45,51 @@ namespace nb = nanobind;
4445
4546namespace {
4647
47- static nb::dict
48- getPyvalFromDynamicShape (const llvm::StringMap<llvm::SmallVector<mlir::Type>> &map) {
49- nb::dict name_to_shape;
50- for (const auto &entry : map) {
51- nb::list types;
52- for (auto type : entry.getValue ()) {
53- if (!type) {
54- continue ;
55- }
56- std::string typestr;
57- llvm::raw_string_ostream ss (typestr);
58- type.print (ss);
59- types.append (typestr);
48+ nb::str getPyvalFromScalarType (mlir::Type type) {
49+ std::string typestr;
50+ llvm::raw_string_ostream ss (typestr);
51+ type.print (ss);
52+ return nb::str (typestr.c_str ());
53+ }
54+
55+ nb::list getPyvalFromShapedType (mlir::ArrayRef<int64_t > shape, int64_t dim, nb::str typestr) {
56+ if (dim == shape.size ()) {
57+ return nb::list ();
58+ }
59+ nb::list result;
60+ for (int i = 0 ; i < shape[dim]; i++) {
61+ result.append (typestr);
62+ }
63+ return result;
64+ }
65+
66+ nb::object getPyvalFromType (mlir::Type type) {
67+ return llvm::TypeSwitch<mlir::Type, nb::object>(type)
68+ .Case <mlir::ShapedType>([&](mlir::ShapedType shapedType) {
69+ return getPyvalFromShapedType (shapedType.getShape (), 0 ,
70+ getPyvalFromScalarType (shapedType.getElementType ()));
71+ })
72+ .Default ([&](mlir::Type other) { return getPyvalFromScalarType (type); });
73+ }
74+
75+ nb::dict getPyvalFromDynamicShape (const llvm::StringMap<llvm::SmallVector<mlir::Type>> &map) {
76+ llvm::SmallVector<llvm::StringRef> keys;
77+ for (const llvm::StringRef key : map.keys ()) {
78+ keys.push_back (key);
79+ }
80+ llvm::sort (keys);
81+
82+ nb::dict result;
83+ for (auto [i, key] : llvm::enumerate (keys)) {
84+ nb::list entry;
85+
86+ const auto &types = map.lookup (key);
87+ for (auto [j, type] : llvm::enumerate (types)) {
88+ entry.append (getPyvalFromType (type));
6089 }
61- name_to_shape[ nb::str (entry. getKey (). str ().c_str ()) ] = types ;
90+ result[key. str ().c_str ()] = entry ;
6291 }
63- return name_to_shape ;
92+ return result ;
6493}
6594
6695static nb::dict getPyvalFromWireLens (const llvm::StringMap<size_t > map) {
0 commit comments