Skip to content

[flang][cuda] Set alloca block in cuf kernel #128776

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
Feb 26, 2025

Conversation

clementval
Copy link
Contributor

Temporary created during lowering in a cuf kernel must be set in the cuf kernel itself otherwise they will be allocated on the host.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 25, 2025

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

Author: Valentin Clement (バレンタイン クレメン) (clementval)

Changes

Temporary created during lowering in a cuf kernel must be set in the cuf kernel itself otherwise they will be allocated on the host.


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

2 Files Affected:

  • (modified) flang/lib/Optimizer/Builder/FIRBuilder.cpp (+3)
  • (added) flang/test/Lower/CUDA/cuda-kernel-alloca-block.cuf (+29)
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index d9779c46ae79e..d6eb6d9336881 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -264,6 +264,9 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
     return recipeIface.getAllocaBlock(getRegion());
   }
 
+  if (auto cufKernelOp = getRegion().getParentOfType<cuf::KernelOp>())
+    return &cufKernelOp.getRegion().front();
+
   return getEntryBlock();
 }
 
diff --git a/flang/test/Lower/CUDA/cuda-kernel-alloca-block.cuf b/flang/test/Lower/CUDA/cuda-kernel-alloca-block.cuf
new file mode 100644
index 0000000000000..afad65bb3138c
--- /dev/null
+++ b/flang/test/Lower/CUDA/cuda-kernel-alloca-block.cuf
@@ -0,0 +1,29 @@
+! RUN: bbc -emit-fir -hlfir -fcuda %s -o - | FileCheck %s
+
+module char1
+
+character*1, dimension(:), allocatable, device :: da, db
+
+contains
+  attributes(device) logical function check_char( c1, c2 )
+    character*1, value :: c1, c2
+    check_char = .false.
+    if(c1 .eq. c2) check_char = .true.
+  end function
+end module
+
+program p
+  use char1
+  integer :: n, i
+  !$cuf kernel do(1)<<<*,*>>>
+  do i = 1, n
+    if (check_char(da(i),db(i))) then
+      print*,'same'
+    endif
+  enddo
+end program
+
+! CHECK-LABEL: func.func @_QQmain()
+! CHECK: cuf.kernel
+! CHECK-COUNT-2: %{{.*}} = fir.alloca !fir.char<1> {bindc_name = ".tmp"}
+

@clementval clementval merged commit e350485 into llvm:main Feb 26, 2025
20 checks passed
@clementval clementval deleted the cuf_alloca_block branch February 26, 2025 01:26
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