@@ -50,47 +50,45 @@ struct ConvertCIRToLLVMPass
5050 StringRef getArgument () const override { return " cir-flat-to-llvm" ; }
5151};
5252
53+ // This pass requires the CIR to be in a "flat" state. All blocks in each
54+ // function must belong to the parent region.
5355mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite (
5456 cir::GlobalOp op, OpAdaptor adaptor,
5557 mlir::ConversionPatternRewriter &rewriter) const {
5658
5759 // Fetch required values to create LLVM op.
5860 const mlir::Type cirSymType = op.getSymType ();
5961
60- // This is the LLVM dialect type
62+ // This is the LLVM dialect type.
6163 const mlir::Type llvmType = getTypeConverter ()->convertType (cirSymType);
62- // These defaults are just here until the equivalent attributes are
63- // available on cir.global ops.
64+ // FIXME: These default values are placeholders until the the equivalent
65+ // attributes are available on cir.global ops.
6466 const bool isConst = false ;
67+ const unsigned addrSpace = 0 ;
6568 const bool isDsoLocal = true ;
69+ const bool isThreadLocal = false ;
70+ const uint64_t alignment = 0 ;
6671 const mlir::LLVM::Linkage linkage = mlir::LLVM::Linkage::External;
6772 const StringRef symbol = op.getSymName ();
6873 std::optional<mlir::Attribute> init = op.getInitialValue ();
6974
7075 SmallVector<mlir::NamedAttribute> attributes;
7176
72- // Check for missing funcionalities.
73- if (!init.has_value ()) {
74- rewriter.replaceOpWithNewOp <mlir::LLVM::GlobalOp>(
75- op, llvmType, isConst, linkage, symbol, mlir::Attribute (),
76- /* alignment*/ 0 , /* addrSpace*/ 0 , /* dsoLocal*/ isDsoLocal,
77- /* threadLocal*/ false , /* comdat*/ mlir::SymbolRefAttr (), attributes);
78- return mlir::success ();
79- }
80-
81- // Initializer is a constant array: convert it to a compatible llvm init.
82- if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(init.value ())) {
83- init = rewriter.getIntegerAttr (llvmType, intAttr.getValue ());
84- } else {
85- op.emitError () << " unsupported initializer '" << init.value () << " '" ;
86- return mlir::failure ();
77+ if (init.has_value ()) {
78+ if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(init.value ())) {
79+ // Initializer is a constant array: convert it to a compatible llvm init.
80+ init = rewriter.getIntegerAttr (llvmType, intAttr.getValue ());
81+ } else {
82+ op.emitError () << " unsupported initializer '" << init.value () << " '" ;
83+ return mlir::failure ();
84+ }
8785 }
8886
8987 // Rewrite op.
9088 rewriter.replaceOpWithNewOp <mlir::LLVM::GlobalOp>(
91- op, llvmType, isConst, linkage, symbol, init.value (), /* alignment */ 0 ,
92- /* addrSpace */ 0 , /* dsoLocal */ isDsoLocal, /* threadLocal */ false ,
93- /* comdat*/ mlir::SymbolRefAttr (), attributes);
89+ op, llvmType, isConst, linkage, symbol, init.value_or ( mlir::Attribute ()) ,
90+ alignment, addrSpace, isDsoLocal, isThreadLocal ,
91+ /* comdat= */ mlir::SymbolRefAttr (), attributes);
9492
9593 return mlir::success ();
9694}
@@ -109,7 +107,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
109107 mlir::ModuleOp module = getOperation ();
110108 mlir::DataLayout dl (module );
111109 mlir::LLVMTypeConverter converter (&getContext ());
112- prepareTypeConverter (converter, dl); // , lowerModule.get());
110+ prepareTypeConverter (converter, dl);
113111
114112 mlir::RewritePatternSet patterns (&getContext ());
115113
@@ -142,22 +140,25 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp mlirModule, LLVMContext &llvmCtx) {
142140 mlir::PassManager pm (mlirCtx);
143141 populateCIRToLLVMPasses (pm);
144142
145- bool result = ! mlir::failed (pm.run (mlirModule));
146- if (!result)
143+ if ( mlir::failed (pm.run (mlirModule))) {
144+ // FIXME: Handle any errors where they occurs and return a nullptr here.
147145 report_fatal_error (
148146 " The pass manager failed to lower CIR to LLVMIR dialect!" );
147+ }
149148
150149 mlir::registerBuiltinDialectTranslation (*mlirCtx);
151150 mlir::registerLLVMDialectTranslation (*mlirCtx);
152151
153152 llvm::TimeTraceScope translateScope (" translateModuleToLLVMIR" );
154153
155- std::optional< StringRef> moduleName = mlirModule.getName ();
154+ StringRef moduleName = mlirModule.getName (). value_or ( " CIRToLLVMModule " );
156155 std::unique_ptr<llvm::Module> llvmModule = mlir::translateModuleToLLVMIR (
157- mlirModule, llvmCtx, moduleName ? *moduleName : " CIRToLLVMModule " );
156+ mlirModule, llvmCtx, moduleName);
158157
159- if (!llvmModule)
158+ if (!llvmModule) {
159+ // FIXME: Handle any errors where they occurs and return a nullptr here.
160160 report_fatal_error (" Lowering from LLVMIR dialect to llvm IR failed!" );
161+ }
161162
162163 return llvmModule;
163164}
0 commit comments