Skip to content

[flang][cuda] Do not generate section checks in device context #146170

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
Jun 27, 2025

Conversation

clementval
Copy link
Contributor

This check is only useful on the host side. Also the function is bot built for the device.

@clementval clementval requested a review from wangzpgi June 27, 2025 22:12
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 27, 2025

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

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

Changes

This check is only useful on the host side. Also the function is bot built for the device.


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

2 Files Affected:

  • (modified) flang/lib/Lower/ConvertCall.cpp (+3-1)
  • (modified) flang/test/Lower/CUDA/cuda-runtime-check.cuf (+26-12)
diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index 864499e6c3431..6ed15df0de754 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -20,6 +20,7 @@
 #include "flang/Lower/StatementContext.h"
 #include "flang/Lower/SymbolMap.h"
 #include "flang/Optimizer/Builder/BoxValue.h"
+#include "flang/Optimizer/Builder/CUFCommon.h"
 #include "flang/Optimizer/Builder/Character.h"
 #include "flang/Optimizer/Builder/FIRBuilder.h"
 #include "flang/Optimizer/Builder/HLFIRTools.h"
@@ -545,7 +546,8 @@ Fortran::lower::genCallOpAndResult(
       caller.getProcedureAttrs(builder.getContext());
 
   if (converter.getLoweringOptions().getCUDARuntimeCheck()) {
-    if (caller.getCallDescription().chevrons().empty()) {
+    if (caller.getCallDescription().chevrons().empty() &&
+        !cuf::isCUDADeviceContext(builder.getRegion())) {
       for (auto [oper, arg] :
            llvm::zip(operands, caller.getPassedArguments())) {
         if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(oper.getType())) {
diff --git a/flang/test/Lower/CUDA/cuda-runtime-check.cuf b/flang/test/Lower/CUDA/cuda-runtime-check.cuf
index f26d372769cab..1aa95ec0ff405 100644
--- a/flang/test/Lower/CUDA/cuda-runtime-check.cuf
+++ b/flang/test/Lower/CUDA/cuda-runtime-check.cuf
@@ -1,22 +1,36 @@
 ! RUN: bbc -emit-hlfir -fcuda %s -o - | FileCheck %s
 
-! Check insertion of runtime checks
+! Check insertion of runtime checks in host side only
+
+module section_test
+
+contains
 
-interface
   subroutine foo(a)
     real, device, dimension(:,:) :: a
   end subroutine
-end interface
 
-  real, device, allocatable, dimension(:,:) :: a
-  allocate(a(10,10))
-  call foo(a(1:10,1:10:2))
-end
+  subroutine test_host()
+    real, device, allocatable, dimension(:,:) :: a
+    allocate(a(10,10))
+    call foo(a(1:10,1:10:2))
+  end subroutine
+
+  attributes(device) subroutine zoo(a)
+    real, device, dimension(:,:) :: a
+  end subroutine
 
-subroutine foo(a)
-  real, device, dimension(:,:) :: a
-end subroutine
+  attributes(global) subroutine test_device()
+    real, device, allocatable, dimension(:,:) :: a
+    allocate(a(10,10))
+    call zoo(a(1:10,1:10:2))
+  end subroutine
+end module
 
-! CHECK-LABEL: func.func @_QQmain()
+! CHECK-LABEL: func.func @_QMsection_testPtest_host()
 ! CHECK: fir.call @_FortranACUFDescriptorCheckSection
-! CHECK: fir.call @_QPfoo
+! CHECK: fir.call @_QMsection_testPfoo
+
+! CHECK-LABEL: func.func @_QMsection_testPtest_device()
+! CHECK-NOT: fir.call @_FortranACUFDescriptorCheckSection
+! CHECK: fir.call @_QMsection_testPzoo

@clementval clementval merged commit 570b952 into llvm:main Jun 27, 2025
10 checks passed
@clementval clementval deleted the cuf_no_check_in_device branch June 27, 2025 23:19
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