Skip to content

Commit 42aca92

Browse files
committed
[Flang] Set address space during FIR pointer-like types lowering
This patch modifies FIR pointer-like types lowering to LLVM dialect to use the address space stated in the module's data layout.
1 parent 935f5ee commit 42aca92

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

flang/include/flang/Optimizer/CodeGen/TypeConverter.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
101101
}
102102

103103
template <typename A> mlir::Type convertPointerLike(A &ty) const {
104-
return mlir::LLVM::LLVMPointerType::get(ty.getContext());
104+
return mlir::LLVM::LLVMPointerType::get(ty.getContext(), addressSpace);
105105
}
106106

107107
// convert a front-end kind value to either a std or LLVM IR dialect type
@@ -127,6 +127,7 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
127127
KindMapping kindMapping;
128128
std::unique_ptr<CodeGenSpecifics> specifics;
129129
std::unique_ptr<TBAABuilder> tbaaBuilder;
130+
unsigned addressSpace;
130131
};
131132

132133
} // namespace fir

flang/lib/Optimizer/CodeGen/TypeConverter.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
3535
getTargetTriple(module),
3636
getKindMapping(module), dl)),
3737
tbaaBuilder(std::make_unique<TBAABuilder>(module->getContext(), applyTBAA,
38-
forceUnifiedTBAATree)) {
38+
forceUnifiedTBAATree)),
39+
addressSpace(0) {
40+
// Get default alloca address space for the current target
41+
if (mlir::Attribute addrSpace =
42+
mlir::DataLayout(module).getAllocaMemorySpace())
43+
addressSpace = addrSpace.cast<mlir::IntegerAttr>().getUInt();
44+
3945
LLVM_DEBUG(llvm::dbgs() << "FIR type converter\n");
4046

4147
// Each conversion should return a value of type mlir::Type.

flang/test/Fir/alloca-addrspace-2.fir

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s
2+
// RUN: tco --fir-to-llvm-ir %s | FileCheck %s
3+
4+
module attributes { dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui32>> } {
5+
// CHECK-LABEL: llvm.func @set_addrspace
6+
func.func @set_addrspace() {
7+
// CHECK: llvm.alloca {{.*}} x i32
8+
// CHECK-SAME: -> !llvm.ptr<i32, 5>
9+
%0 = fir.alloca i32
10+
return
11+
}
12+
}

flang/test/Fir/alloca-addrspace.fir

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: fir-opt --fir-to-llvm-ir %s | FileCheck %s
2+
// RUN: tco --fir-to-llvm-ir %s | FileCheck %s
3+
4+
module {
5+
// CHECK-LABEL: llvm.func @default_addrspace
6+
func.func @default_addrspace() {
7+
// CHECK: llvm.alloca {{.*}} x i32
8+
// CHECK-SAME: -> !llvm.ptr<i32>
9+
%0 = fir.alloca i32
10+
return
11+
}
12+
}

0 commit comments

Comments
 (0)