@@ -48,7 +48,9 @@ heavy::ExternFunction block_op;
4848heavy::ExternFunction set_insertion_point;
4949heavy::ExternFunction set_insertion_after;
5050heavy::ExternFunction type;
51+ heavy::ExternFunction function_type_impl;
5152heavy::ExternFunction attr;
53+ heavy::ExternFunction type_attr;
5254heavy::ExternFunction value_attr;
5355template <typename AttrTy>
5456heavy::ExternFunction string_attr;
@@ -58,6 +60,7 @@ heavy::ExternFunction load_dialect;
5860heavy::ExternFunction parent_op;
5961heavy::ExternFunction op_next;
6062heavy::ExternFunction verify;
63+ heavy::ExternFunction module_lookup;
6164}
6265
6366namespace {
@@ -577,6 +580,45 @@ void type(Context& C, ValueRefs Args) {
577580 C.Cont (CreateTagged (C, kind::mlir_type, Type.getImpl ()));
578581}
579582
583+ // Create a function type (using vector literals.
584+ // (%function-type #(<arg-types>...) #(<result-types>...))
585+ void function_type_impl (Context& C, ValueRefs Args) {
586+ mlir::MLIRContext* MLIRContext = getCurrentContext (C);
587+ if (Args.size () != 2 )
588+ return C.RaiseError (" invalid arity" );
589+ auto * ArgTypeVals = heavy::dyn_cast<heavy::Vector>(Args[0 ]);
590+ auto * ResultTypeVals = heavy::dyn_cast<heavy::Vector>(Args[1 ]);
591+ if (!ArgTypeVals || !ResultTypeVals)
592+ return C.RaiseError (" expecting vectors" );
593+
594+ // Arg types
595+ llvm::SmallVector<mlir::Type, 8 > ArgTypes;
596+ Args = Args.drop_front ();
597+ for (heavy::Value Arg : ArgTypeVals->getElements ()) {
598+ mlir::Type ArgType = GetTagged<mlir::Type>(C, kind::mlir_type, Arg);
599+ if (!ArgType)
600+ return C.RaiseError (" expecting mlir.type" );
601+ ArgTypes.push_back (ArgType);
602+ }
603+
604+ // Result types
605+ llvm::SmallVector<mlir::Type, 8 > ResultTypes;
606+ for (heavy::Value Result : ResultTypeVals->getElements ()) {
607+ mlir::Type ResultType = GetTagged<mlir::Type>(C, kind::mlir_type, Result);
608+ if (!ResultType)
609+ return C.RaiseError (" expecting mlir.type" );
610+ ResultTypes.push_back (ResultType);
611+ }
612+
613+ mlir::Type Type = mlir::FunctionType::get (MLIRContext, ArgTypes,
614+ ResultTypes);
615+
616+ if (!Type)
617+ return C.RaiseError (" mlir build function type failed" );
618+
619+ C.Cont (CreateTagged (C, kind::mlir_type, Type.getImpl ()));
620+ }
621+
580622// Get an attribute by parsing a string.
581623// Usage: (attr _attr_str [_type_])
582624// attr_str - the string to be parsed
@@ -621,6 +663,17 @@ void attr(Context& C, ValueRefs Args) {
621663 C.Cont (CreateTagged (C, kind::mlir_attr, Attr.getImpl ()));
622664}
623665
666+ // (type-attr (type "!type-goes-here"))
667+ void type_attr (Context& C, ValueRefs Args) {
668+ if (Args.size () != 1 )
669+ return C.RaiseError (" invalid arity" );
670+ mlir::Type Type = GetTagged<mlir::Type>(C, kind::mlir_type, Args[0 ]);
671+ if (!Type)
672+ return C.RaiseError (" expecting a mlir.type" );
673+ mlir::Attribute Attr = mlir::TypeAttr::get (Type);
674+ C.Cont (CreateTagged (C, kind::mlir_attr, Attr.getImpl ()));
675+ }
676+
624677// Create a heavy scheme value attribute of type !heavy.value
625678void value_attr (Context& C, ValueRefs Args) {
626679 if (Args.size () != 1 )
@@ -769,6 +822,24 @@ void verify(Context& C, heavy::ValueRefs Args) {
769822 }
770823}
771824
825+ // (symbol-table-lookup _module-op_ _"symbolname"_)
826+ void module_lookup (Context& C, heavy::ValueRefs Args) {
827+ if (Args.size () != 2 )
828+ return C.RaiseError (" invalid arity" );
829+
830+ mlir::ModuleOp ModuleOp = dyn_cast_or_null<mlir::ModuleOp>(
831+ dyn_cast<mlir::Operation>(Args[0 ]));
832+ llvm::StringRef SymbolName = Args[1 ].getStringRef ();
833+ if (!ModuleOp)
834+ return C.RaiseError (" expecting mlir::ModuleOp" );
835+ if (SymbolName.empty ())
836+ return C.RaiseError (" expecting nonempty string-like object" );
837+
838+ mlir::Operation* Op = ModuleOp.lookupSymbol (SymbolName);
839+ heavy::Value Result = Op != nullptr ? heavy::Value (Op) : heavy::Empty ();
840+ C.Cont (Result);
841+ }
842+
772843} // namespace heavy::mlir_bind
773844
774845extern " C" {
@@ -795,7 +866,9 @@ void HEAVY_MLIR_INIT(heavy::Context& C) {
795866 HEAVY_MLIR_VAR (set_insertion_point) = heavy::mlir_bind::set_insertion_point;
796867 HEAVY_MLIR_VAR (set_insertion_after) = heavy::mlir_bind::set_insertion_after;
797868 HEAVY_MLIR_VAR (type) = heavy::mlir_bind::type;
869+ HEAVY_MLIR_VAR (function_type_impl) = heavy::mlir_bind::function_type_impl;
798870 HEAVY_MLIR_VAR (attr) = heavy::mlir_bind::attr;
871+ HEAVY_MLIR_VAR (type_attr) = heavy::mlir_bind::type_attr;
799872 HEAVY_MLIR_VAR (value_attr) = heavy::mlir_bind::value_attr;
800873 HEAVY_MLIR_VAR (string_attr<mlir::StringAttr>)
801874 = heavy::mlir_bind::string_attr<mlir::StringAttr>;
@@ -805,6 +878,7 @@ void HEAVY_MLIR_INIT(heavy::Context& C) {
805878 HEAVY_MLIR_VAR (with_builder) = heavy::mlir_bind::with_builder;
806879 HEAVY_MLIR_VAR (with_new_context) = heavy::mlir_bind::with_new_context;
807880 HEAVY_MLIR_VAR (verify) = heavy::mlir_bind::verify;
881+ HEAVY_MLIR_VAR (module_lookup) = heavy::mlir_bind::module_lookup;
808882}
809883
810884void HEAVY_MLIR_LOAD_MODULE (heavy::Context& C) {
@@ -825,7 +899,9 @@ void HEAVY_MLIR_LOAD_MODULE(heavy::Context& C) {
825899 {" set-insertion-point" , HEAVY_MLIR_VAR (set_insertion_point)},
826900 {" set-insertion-after" , HEAVY_MLIR_VAR (set_insertion_after)},
827901 {" type" , HEAVY_MLIR_VAR (type)},
902+ {" %function-type" , HEAVY_MLIR_VAR (function_type_impl)},
828903 {" attr" , HEAVY_MLIR_VAR (attr)},
904+ {" type-attr" , HEAVY_MLIR_VAR (type_attr)},
829905 {" value-attr" , HEAVY_MLIR_VAR (value_attr)},
830906 {" string-attr" , HEAVY_MLIR_VAR (string_attr<mlir::StringAttr>)},
831907 {" flat-symbolref-attr" ,
@@ -834,6 +910,7 @@ void HEAVY_MLIR_LOAD_MODULE(heavy::Context& C) {
834910 {" with-builder" , HEAVY_MLIR_VAR (with_builder)},
835911 {" load-dialect" , HEAVY_MLIR_VAR (load_dialect)},
836912 {" verify" , HEAVY_MLIR_VAR (verify)},
913+ {" module-lookup" , HEAVY_MLIR_VAR (module_lookup)},
837914 });
838915}
839916}
0 commit comments