[Mlir-commits] [mlir] 8c2ff7b - [MLIR] Correct linkage of lowered globalop

William S. Moses llvmlistbot at llvm.org
Wed Aug 18 08:09:59 PDT 2021


Author: William S. Moses
Date: 2021-08-18T11:09:43-04:00
New Revision: 8c2ff7b69e77a9eccf4a13bcc21dee5894b251af

URL: https://github.com/llvm/llvm-project/commit/8c2ff7b69e77a9eccf4a13bcc21dee5894b251af
DIFF: https://github.com/llvm/llvm-project/commit/8c2ff7b69e77a9eccf4a13bcc21dee5894b251af.diff

LOG: [MLIR] Correct linkage of lowered globalop

LLVM considers global variables marked as externals to be defined within the module if it is initialized (including to an undef). Other external globals are considered as being defined externally and imported into the current translation unit. Lowering of MLIR Global Ops does not properly propagate undefined initializers, resulting in a global which is expected to be defined within the current TU, not being defined.

Differential Revision: https://reviews.llvm.org/D108252

Added: 
    

Modified: 
    mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
    mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index 2327fa0807827..e3aaef5cf0cbd 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -454,9 +454,17 @@ struct GlobalMemrefOpLowering
         initialValue = elementsAttr.getValue({});
     }
 
-    rewriter.replaceOpWithNewOp<LLVM::GlobalOp>(
+    auto newGlobal = rewriter.replaceOpWithNewOp<LLVM::GlobalOp>(
         global, arrayTy, global.constant(), linkage, global.sym_name(),
         initialValue, /*alignment=*/0, type.getMemorySpaceAsInt());
+    if (!global.isExternal() && global.isUninitialized()) {
+      Block *blk = new Block();
+      newGlobal.getInitializerRegion().push_back(blk);
+      rewriter.setInsertionPointToStart(blk);
+      Value undef[] = {
+          rewriter.create<LLVM::UndefOp>(global.getLoc(), arrayTy)};
+      rewriter.create<LLVM::ReturnOp>(global.getLoc(), undef);
+    }
     return success();
   }
 };

diff  --git a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
index 4344f88bea5e9..8e5f3dd5a4ac9 100644
--- a/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
+++ b/mlir/test/Conversion/MemRefToLLVM/memref-to-llvm.mlir
@@ -623,7 +623,10 @@ func @transpose(%arg0: memref<?x?x?xf32, offset: ?, strides: [?, ?, 1]>) {
 
 // -----
 
-// CHECK: llvm.mlir.global external @gv0() : !llvm.array<2 x f32>
+// CHECK:   llvm.mlir.global external @gv0() : !llvm.array<2 x f32> {
+// CHECK-NEXT:     %0 = llvm.mlir.undef : !llvm.array<2 x f32>
+// CHECK-NEXT:     llvm.return %0 : !llvm.array<2 x f32>
+// CHECK-NEXT:   }
 memref.global @gv0 : memref<2xf32> = uninitialized
 
 // CHECK: llvm.mlir.global private @gv1() : !llvm.array<2 x f32>


        


More information about the Mlir-commits mailing list