From e79bfefb89201de52b9c8d609b67b416d3982977 Mon Sep 17 00:00:00 2001 From: MLIR Team Date: Thu, 19 Sep 2019 04:50:17 -0700 Subject: [PATCH] Add address space attribute to LLVMIR's GlobalOp. PiperOrigin-RevId: 270012505 --- mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td | 3 ++- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp | 9 ++++++--- mlir/test/Dialect/LLVMIR/global.mlir | 10 ++++++++++ mlir/test/Target/llvmir.mlir | 3 +++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td index d90a232..170246f 100644 --- a/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td +++ b/mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td @@ -487,7 +487,8 @@ def LLVM_AddressOfOp def LLVM_GlobalOp : LLVM_ZeroResultOp<"mlir.global">, Arguments<(ins TypeAttr:$type, UnitAttr:$constant, StrAttr:$sym_name, - AnyAttr:$value)> { + AnyAttr:$value, + DefaultValuedAttr:$addr_space)> { let builders = [ OpBuilder<"Builder *builder, OperationState *result, LLVMType type, " diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp index cbdc6c2..5b5c06a 100644 --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -326,9 +326,12 @@ void ModuleTranslation::convertGlobals() { cst = getLLVMConstant(type, op.value(), op.getLoc()); } - auto *var = new llvm::GlobalVariable(*llvmModule, type, op.constant(), - llvm::GlobalValue::InternalLinkage, - cst, op.sym_name()); + auto addrSpace = op.addr_space().getLimitedValue(); + auto *var = new llvm::GlobalVariable( + *llvmModule, type, op.constant(), llvm::GlobalValue::InternalLinkage, + cst, op.sym_name(), /*InsertBefore=*/nullptr, + llvm::GlobalValue::NotThreadLocal, addrSpace); + globalsMapping.try_emplace(op, var); } } diff --git a/mlir/test/Dialect/LLVMIR/global.mlir b/mlir/test/Dialect/LLVMIR/global.mlir index 40534eb..a9e2ddb 100644 --- a/mlir/test/Dialect/LLVMIR/global.mlir +++ b/mlir/test/Dialect/LLVMIR/global.mlir @@ -45,6 +45,16 @@ llvm.mlir.global constant @constant(37.0) : !llvm<"label"> // ----- +// expected-error @+1 {{'addr_space' failed to satisfy constraint: non-negative 32-bit integer}} +"llvm.mlir.global"() {sym_name = "foo", type = !llvm.i64, value = 42 : i64, addr_space = -1 : i32} : () -> () + +// ----- + +// expected-error @+1 {{'addr_space' failed to satisfy constraint: non-negative 32-bit integer}} +"llvm.mlir.global"() {sym_name = "foo", type = !llvm.i64, value = 42 : i64, addr_space = 1.0 : f32} : () -> () + +// ----- + func @foo() { // expected-error @+1 {{must appear at the module level}} llvm.mlir.global @bar(42) : !llvm.i32 diff --git a/mlir/test/Target/llvmir.mlir b/mlir/test/Target/llvmir.mlir index cf365c1..c37e68c 100644 --- a/mlir/test/Target/llvmir.mlir +++ b/mlir/test/Target/llvmir.mlir @@ -9,6 +9,9 @@ llvm.mlir.global constant @i32_const(52: i53) : !llvm.i53 // CHECK: @int_global_array = internal global [3 x i32] [i32 62, i32 62, i32 62] llvm.mlir.global @int_global_array(dense<62> : vector<3xi32>) : !llvm<"[3 x i32]"> +// CHECK: @i32_global_addr_space = internal addrspace(7) global i32 62 +llvm.mlir.global @i32_global_addr_space(62: i32) {addr_space = 7 : i32} : !llvm.i32 + // CHECK: @float_global = internal global float 0.000000e+00 llvm.mlir.global @float_global(0.0: f32) : !llvm.float -- 2.7.4