Skip to content

[Flang][Transform] Modify stack reclaim pass to use allocation address space when generating intrinsics #96836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2024

Conversation

agozillon
Copy link
Contributor

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.

…s space when generating intrinsics

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: llvm#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.
@agozillon agozillon requested a review from clementval June 27, 2024 01:46
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (agozillon)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/96836.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/StackReclaim.cpp (+13-1)
  • (modified) flang/test/Transforms/stack-reclaime.fir (+17)
diff --git a/flang/lib/Optimizer/Transforms/StackReclaim.cpp b/flang/lib/Optimizer/Transforms/StackReclaim.cpp
index e5e0e4eab8298..8a60a9e64f704 100644
--- a/flang/lib/Optimizer/Transforms/StackReclaim.cpp
+++ b/flang/lib/Optimizer/Transforms/StackReclaim.cpp
@@ -31,11 +31,23 @@ class StackReclaimPass : public fir::impl::StackReclaimBase<StackReclaimPass> {
 };
 } // namespace
 
+uint64_t getAllocaAddressSpace(Operation *op) {
+  mlir::ModuleOp module = mlir::dyn_cast_or_null<mlir::ModuleOp>(op);
+  if (!module)
+    module = op->getParentOfType<mlir::ModuleOp>();
+
+  if (mlir::Attribute addrSpace =
+          mlir::DataLayout(module).getAllocaMemorySpace())
+    return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt();
+  return 0;
+}
+
 void StackReclaimPass::runOnOperation() {
   auto *op = getOperation();
   auto *context = &getContext();
   mlir::OpBuilder builder(context);
-  mlir::Type voidPtr = mlir::LLVM::LLVMPointerType::get(context);
+  mlir::Type voidPtr =
+      mlir::LLVM::LLVMPointerType::get(context, getAllocaAddressSpace(op));
 
   op->walk([&](fir::DoLoopOp loopOp) {
     mlir::Location loc = loopOp.getLoc();
diff --git a/flang/test/Transforms/stack-reclaime.fir b/flang/test/Transforms/stack-reclaime.fir
index b53cc96035751..96416df8b2013 100644
--- a/flang/test/Transforms/stack-reclaime.fir
+++ b/flang/test/Transforms/stack-reclaime.fir
@@ -12,3 +12,20 @@ func.func @alloca_in_loop(%lb : index, %ub : index, %step : index, %b : i1, %add
 // CHECK: %[[STACKPTR:.*]] = llvm.intr.stacksave : !llvm.ptr
 // CHECK: %{{.*}} = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
 // CHECK: llvm.intr.stackrestore %0 : !llvm.ptr
+
+// -----
+
+module attributes {dlti.dl_spec = #dlti.dl_spec<#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>>} {
+  func.func @stack_restore_save_alloca_address(%lb : index, %ub : index, %step : index, %b : i1, %addr : !fir.ref<index>) {
+    fir.do_loop %iv = %lb to %ub step %step unordered {
+      %0 = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
+    }
+    return
+  }
+}
+
+// CHECK-LABEL: func.func @stack_restore_save_alloca_address
+// CHECK: fir.do_loop
+// CHECK: %[[STACKPTR:.*]] = llvm.intr.stacksave : !llvm.ptr<5>
+// CHECK: %{{.*}} = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
+// CHECK: llvm.intr.stackrestore %0 : !llvm.ptr<5>

@agozillon
Copy link
Contributor Author

Small ping for some reviewer attention please, if at all possible! :-) thank you very much in advance!

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@agozillon
Copy link
Contributor Author

@jeanPerier thank you very much for the review! :-)

I will merge the PR later today or tomorrow if no further comments are made.

@agozillon agozillon merged commit a1bc606 into llvm:main Jul 3, 2024
10 checks passed
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
…s space when generating intrinsics (llvm#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:
llvm#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants