Skip to content

Commit a1bc606

Browse files
authored
[Flang][Transform] Modify stack reclaim pass to use allocation address space when generating intrinsics (#96836)
This PR aims to factor in the allocation address space provided by an architectures data layout when generating the intrinsic instructions, this allows them to be lowered later with the address spaces in tow. This aligns the intrinsic creation with the LLVM IRBuilder's https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/IRBuilder.h#L1053 This is also necessary for the below example to compile for OpenMP AMD GPU and not ICE the compiler in ISEL as AMD's stackrestore and stacksave are expected to have the appropriate allocation address space for AMD GPU. program main integer(4), allocatable :: test allocate(test) !$omp target map(tofrom:test) do i = 1, 10 test = test + 50 end do !$omp end target deallocate(test) end program The PR also fixes the issue I opened a while ago which hits the same error when compiling for AMDGPU: #82368 Although, you have to have the appropriate GPU LIBC and Fortran offload runtime (both compiled for AMDGPU) added to the linker for the command or it will reach another ISEL error and ICE weirdly. But with the pre-requisites it works fine with this PR.
1 parent 7002ecb commit a1bc606

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

flang/lib/Optimizer/Transforms/StackReclaim.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,23 @@ class StackReclaimPass : public fir::impl::StackReclaimBase<StackReclaimPass> {
3131
};
3232
} // namespace
3333

34+
uint64_t getAllocaAddressSpace(Operation *op) {
35+
mlir::ModuleOp module = mlir::dyn_cast_or_null<mlir::ModuleOp>(op);
36+
if (!module)
37+
module = op->getParentOfType<mlir::ModuleOp>();
38+
39+
if (mlir::Attribute addrSpace =
40+
mlir::DataLayout(module).getAllocaMemorySpace())
41+
return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt();
42+
return 0;
43+
}
44+
3445
void StackReclaimPass::runOnOperation() {
3546
auto *op = getOperation();
3647
auto *context = &getContext();
3748
mlir::OpBuilder builder(context);
38-
mlir::Type voidPtr = mlir::LLVM::LLVMPointerType::get(context);
49+
mlir::Type voidPtr =
50+
mlir::LLVM::LLVMPointerType::get(context, getAllocaAddressSpace(op));
3951

4052
op->walk([&](fir::DoLoopOp loopOp) {
4153
mlir::Location loc = loopOp.getLoc();

flang/test/Transforms/stack-reclaime.fir

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,20 @@ func.func @alloca_in_loop(%lb : index, %ub : index, %step : index, %b : i1, %add
1212
// CHECK: %[[STACKPTR:.*]] = llvm.intr.stacksave : !llvm.ptr
1313
// CHECK: %{{.*}} = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
1414
// CHECK: llvm.intr.stackrestore %0 : !llvm.ptr
15+
16+
// -----
17+
18+
module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>>} {
19+
func.func @stack_restore_save_alloca_address(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
20+
fir.do_loop %iv = %lb to %ub step %step unordered {
21+
%0 = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
22+
}
23+
return
24+
}
25+
}
26+
27+
// CHECK-LABEL: func.func @stack_restore_save_alloca_address
28+
// CHECK: fir.do_loop
29+
// CHECK: %[[STACKPTR:.*]] = llvm.intr.stacksave : !llvm.ptr<5>
30+
// CHECK: %{{.*}} = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
31+
// CHECK: llvm.intr.stackrestore %0 : !llvm.ptr<5>

0 commit comments

Comments
 (0)