[MLIR] Add and propagate section attribute for LLVM_GlobalOp
authorRanjith Kumar H <ranjithkh1@gmail.com>
Wed, 28 Apr 2021 03:59:11 +0000 (03:59 +0000)
committerMehdi Amini <joker.eph@gmail.com>
Wed, 28 Apr 2021 04:15:49 +0000 (04:15 +0000)
Add a section attribute to LLVM_GlobalOp, during module translation attribute value is propagated to llvm

Reviewed By: sgrechanik, ftynse, mehdi_amini

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

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td
mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp
mlir/lib/Target/LLVMIR/ModuleTranslation.cpp
mlir/test/Dialect/LLVMIR/global.mlir
mlir/test/Target/LLVMIR/import.ll
mlir/test/Target/LLVMIR/llvmir.mlir

index 1ff060a..c379dc7 100644 (file)
@@ -908,7 +908,8 @@ def LLVM_GlobalOp : LLVM_Op<"mlir.global",
     Linkage:$linkage,
     OptionalAttr<AnyAttr>:$value,
     DefaultValuedAttr<Confined<I32Attr, [IntNonNegative]>, "0">:$addr_space,
-    OptionalAttr<UnnamedAddr>:$unnamed_addr
+    OptionalAttr<UnnamedAddr>:$unnamed_addr,
+    OptionalAttr<StrAttr>:$section
   );
   let summary = "LLVM dialect global.";
   let description = [{
index e1e57bb..5b1faee 100644 (file)
@@ -490,6 +490,8 @@ GlobalOp Importer::processGlobal(llvm::GlobalVariable *GV) {
   if (GV->hasAtLeastLocalUnnamedAddr())
     op.unnamed_addrAttr(UnnamedAddrAttr::get(
         context, convertUnnamedAddrFromLLVM(GV->getUnnamedAddr())));
+  if (GV->hasSection())
+    op.sectionAttr(b.getStringAttr(GV->getSection()));
   return globals[GV] = op;
 }
 
index 33d439d..ea4c32c 100644 (file)
@@ -410,6 +410,9 @@ LogicalResult ModuleTranslation::convertGlobals() {
     if (op.unnamed_addr().hasValue())
       var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.unnamed_addr()));
 
+    if (op.section().hasValue())
+      var->setSection(*op.section());
+
     globalsMapping.try_emplace(op, var);
   }
 
index 23f77ab..a5306c4 100644 (file)
@@ -69,6 +69,9 @@ llvm.mlir.global private local_unnamed_addr constant @local(42 : i64) : i64
 // CHECK: llvm.mlir.global private unnamed_addr constant @foo(42 : i64) : i64
 llvm.mlir.global private unnamed_addr constant @foo(42 : i64) : i64
 
+// CHECK: llvm.mlir.global internal constant @sectionvar("teststring")  {section = ".mysection"}
+llvm.mlir.global internal constant @sectionvar("teststring")  {section = ".mysection"}: !llvm.array<10 x i8>
+
 // -----
 
 // expected-error @+1 {{requires string attribute 'sym_name'}}
index c208b76..027bc29 100644 (file)
 @unnamed_addr = private unnamed_addr constant i64 42
 
 ;
+; Section attribute
+;
+
+; CHECK: llvm.mlir.global internal constant @sectionvar("teststring")  {section = ".mysection"}
+@sectionvar = internal constant [10 x i8] c"teststring", section ".mysection"
+
+;
 ; Sequential constants.
 ;
 
index abaec73..2b59893 100644 (file)
@@ -71,6 +71,13 @@ llvm.mlir.global private local_unnamed_addr constant @local_unnamed_addr(42 : i6
 llvm.mlir.global private unnamed_addr constant @unnamed_addr(42 : i64) : i64
 
 //
+// Section attribute.
+//
+
+// CHECK: @sectionvar = internal constant [10 x i8] c"teststring", section ".mysection"
+llvm.mlir.global internal constant @sectionvar("teststring")  {section = ".mysection"}: !llvm.array<10 x i8>
+
+//
 // Declarations of the allocation functions to be linked against. These are
 // inserted before other functions in the module.
 //