diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -908,7 +908,8 @@ Linkage:$linkage, OptionalAttr:$value, DefaultValuedAttr, "0">:$addr_space, - OptionalAttr:$unnamed_addr + OptionalAttr:$unnamed_addr, + OptionalAttr:$section ); let summary = "LLVM dialect global."; let description = [{ diff --git a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp --- a/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp +++ b/mlir/lib/Target/LLVMIR/ConvertFromLLVMIR.cpp @@ -490,6 +490,8 @@ 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; } diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -410,6 +410,9 @@ if (op.unnamed_addr().hasValue()) var->setUnnamedAddr(convertUnnamedAddrToLLVM(*op.unnamed_addr())); + if (op.section().hasValue()) + var->setSection(*op.section()); + globalsMapping.try_emplace(op, var); } diff --git a/mlir/test/Dialect/LLVMIR/global.mlir b/mlir/test/Dialect/LLVMIR/global.mlir --- a/mlir/test/Dialect/LLVMIR/global.mlir +++ b/mlir/test/Dialect/LLVMIR/global.mlir @@ -69,6 +69,9 @@ // 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'}} diff --git a/mlir/test/Target/LLVMIR/import.ll b/mlir/test/Target/LLVMIR/import.ll --- a/mlir/test/Target/LLVMIR/import.ll +++ b/mlir/test/Target/LLVMIR/import.ll @@ -61,6 +61,13 @@ ; CHECK: llvm.mlir.global private unnamed_addr constant @unnamed_addr(42 : i64) : i64 @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. ; diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir --- a/mlir/test/Target/LLVMIR/llvmir.mlir +++ b/mlir/test/Target/LLVMIR/llvmir.mlir @@ -70,6 +70,13 @@ // CHECK: @unnamed_addr = private unnamed_addr constant i64 42 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.