Skip to content

[AMDGPU] Add verification for amdgcn.init.exec.from.input #128172

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 7 commits into from
Feb 23, 2025

Conversation

ro-i
Copy link
Contributor

@ro-i ro-i commented Feb 21, 2025

Check that the input register is an inreg argument to the parent function. (See the comment in IntrinsicsAMDGPU.td.)

This LLVM defect was identified via the AMD Fuzzing project.

Check that the input register is an inreg argument to the parent
function. (See the comment in `IntrinsicsAMDGPU.td`.)

This LLVM defect was identified via the AMD Fuzzing project.
@llvmbot
Copy link
Member

llvmbot commented Feb 21, 2025

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-ir

Author: Robert Imschweiler (ro-i)

Changes

Check that the input register is an inreg argument to the parent function. (See the comment in IntrinsicsAMDGPU.td.)

This LLVM defect was identified via the AMD Fuzzing project.


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

2 Files Affected:

  • (modified) llvm/lib/IR/Verifier.cpp (+15)
  • (added) llvm/test/Verifier/AMDGPU/intrinsic-amdgpu-init-exec-from-input.ll (+27)
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 8432779c107de..55923fd56a109 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -6369,6 +6369,21 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
           "VGPR arguments must not have the `inreg` attribute", &Call);
     break;
   }
+  case Intrinsic::amdgcn_init_exec_from_input: {
+    const Value *InputVal = Call.getOperand(0);
+    bool InRegArgFound = false;
+    for (const Argument &Arg : Call.getCaller()->args()) {
+      if (Arg.hasInRegAttr() && &Arg == InputVal) {
+        InRegArgFound = true;
+        break;
+      }
+    }
+    Check(InRegArgFound,
+          "only inreg arguments to the parent function are valid as inputs to "
+          "this intrinsic",
+          &Call);
+    break;
+  }
   case Intrinsic::amdgcn_set_inactive_chain_arg: {
     auto CallerCC = Call.getCaller()->getCallingConv();
     switch (CallerCC) {
diff --git a/llvm/test/Verifier/AMDGPU/intrinsic-amdgpu-init-exec-from-input.ll b/llvm/test/Verifier/AMDGPU/intrinsic-amdgpu-init-exec-from-input.ll
new file mode 100644
index 0000000000000..5e4a6638dc170
--- /dev/null
+++ b/llvm/test/Verifier/AMDGPU/intrinsic-amdgpu-init-exec-from-input.ll
@@ -0,0 +1,27 @@
+; RUN: sed -e '/^; MARK$/,$d' %s | not llc -mtriple=amdgcn -mcpu=gfx942 2>&1 | FileCheck %s
+; RUN: sed -e '1,/^; MARK$/d' %s | llc -mtriple=amdgcn -mcpu=gfx942 -filetype=null
+
+; Function Attrs: convergent nocallback nofree nounwind willreturn
+declare void @llvm.amdgcn.init.exec.from.input(i32, i32 immarg) #0
+attributes #0 = { convergent nocallback nofree nounwind willreturn }
+
+; CHECK: only inreg arguments to the parent function are valid as inputs to this intrinsic
+; CHECK-NEXT: call void @llvm.amdgcn.init.exec.from.input(i32 0, i32 0)
+define amdgpu_ps void @init_exec_from_input_fail_immarg(i32 inreg %a, i32 %b) {
+  call void @llvm.amdgcn.init.exec.from.input(i32 0, i32 0)
+  ret void
+}
+
+; CHECK: only inreg arguments to the parent function are valid as inputs to this intrinsic
+; CHECK-NEXT: call void @llvm.amdgcn.init.exec.from.input(i32 %b, i32 0)
+define amdgpu_ps void @init_exec_from_input_fail_not_inreg(i32 inreg %a, i32 %b) {
+  call void @llvm.amdgcn.init.exec.from.input(i32 %b, i32 0)
+  ret void
+}
+
+; MARK
+
+define amdgpu_ps void @init_exec_from_input_success(i32 inreg %a, i32 %b) {
+  call void @llvm.amdgcn.init.exec.from.input(i32 %a, i32 0)
+  ret void
+}

@@ -18,10 +17,3 @@ define amdgpu_ps void @init_exec_from_input_fail_not_inreg(i32 inreg %a, i32 %b)
call void @llvm.amdgcn.init.exec.from.input(i32 %b, i32 0)
ret void
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Also test a case where it's not an argument (try one with a constant and one with an instruction)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I added another check for the case that the operand is an instruction!
Isn't the constant case covered by the first function?

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

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

Don't know why we have a separate intrinsic for the case where the value happens to be an argument or not

@shiltian shiltian merged commit cafad2b into llvm:main Feb 23, 2025
11 checks passed
@ro-i ro-i deleted the verify-amdgpu-init-exec branch February 23, 2025 16:11
@llvm-ci
Copy link
Collaborator

llvm-ci commented Feb 23, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-win-x-armv7l running on as-builder-1 while building llvm at step 10 "test-check-clang".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/38/builds/2473

Here is the relevant piece of the build log for the reference
Step 10 (test-check-clang) failure: Test just built components: check-clang completed (failure)
******************** TEST 'Clang :: Driver/offload-Xarch.c' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 3
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe --target=x86_64-unknown-linux-gnu -x cuda C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c -Xarch_nvptx64 -O3 -S -nogpulib -nogpuinc -### 2>&1 | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=O3ONCE C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' --target=x86_64-unknown-linux-gnu -x cuda 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c' -Xarch_nvptx64 -O3 -S -nogpulib -nogpuinc '-###'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=O3ONCE 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 4
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -x cuda C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c -Xarch_device -O3 -S -nogpulib -nogpuinc -### 2>&1 | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=O3ONCE C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -x cuda 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c' -Xarch_device -O3 -S -nogpulib -nogpuinc '-###'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=O3ONCE 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 5
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -x hip C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c -Xarch_amdgcn -O3 -S -nogpulib -nogpuinc -### 2>&1 | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=O3ONCE C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -x hip 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c' -Xarch_amdgcn -O3 -S -nogpulib -nogpuinc '-###'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=O3ONCE 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 6
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -fopenmp=libomp -fopenmp-targets=amdgcn-amd-amdhsa -nogpulib -nogpuinc    -Xarch_amdgcn -march=gfx90a -Xarch_amdgcn -O3 -S -### C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c 2>&1  | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=O3ONCE C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -fopenmp=libomp -fopenmp-targets=amdgcn-amd-amdhsa -nogpulib -nogpuinc -Xarch_amdgcn -march=gfx90a -Xarch_amdgcn -O3 -S '-###' 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=O3ONCE 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 9
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -nogpulib -nogpuinc    -Xarch_nvptx64 -march=sm_52 -Xarch_nvptx64 -O3 -S -### C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c 2>&1  | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=O3ONCE C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda -nogpulib -nogpuinc -Xarch_nvptx64 -march=sm_52 -Xarch_nvptx64 -O3 -S '-###' 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=O3ONCE 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 15
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -nogpulib    --target=x86_64-unknown-linux-gnu -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_52,sm_60 -nogpuinc    -Xopenmp-target=amdgcn-amd-amdhsa --offload-arch=gfx90a,gfx1030 -ccc-print-bindings -### C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c 2>&1  | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=OPENMP C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -fopenmp=libomp -fopenmp-targets=nvptx64-nvidia-cuda,amdgcn-amd-amdhsa -nogpulib --target=x86_64-unknown-linux-gnu -Xopenmp-target=nvptx64-nvidia-cuda --offload-arch=sm_52,sm_60 -nogpuinc -Xopenmp-target=amdgcn-amd-amdhsa --offload-arch=gfx90a,gfx1030 -ccc-print-bindings '-###' 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=OPENMP 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 31
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -x cuda C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c --offload-arch=sm_52,sm_60 -Xarch_sm_52 -O3 -Xarch_sm_60 -O0    --target=x86_64-unknown-linux-gnu -Xarch_host -O3 -S -nogpulib -nogpuinc -### 2>&1  | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=CUDA C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -x cuda 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c' --offload-arch=sm_52,sm_60 -Xarch_sm_52 -O3 -Xarch_sm_60 -O0 --target=x86_64-unknown-linux-gnu -Xarch_host -O3 -S -nogpulib -nogpuinc '-###'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=CUDA 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# RUN: at line 39
c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc    --target=x86_64-unknown-linux-gnu -Xarch_amdgcn -Wl,-lfoo -### C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c 2>&1  | c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe -check-prefix=LIBS C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\clang.exe' -fopenmp=libomp --offload-arch=gfx90a -nogpulib -nogpuinc --target=x86_64-unknown-linux-gnu -Xarch_amdgcn -Wl,-lfoo '-###' 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# executed command: 'c:\buildbot\as-builder-1\x-armv7l\build\bin\filecheck.exe' -check-prefix=LIBS 'C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c'
# .---command stderr------------
# | C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c:45:10: error: LIBS: expected string not found in input
# | // LIBS: "--device-linker=amdgcn-amd-amdhsa=-lfoo"
# |          ^
# | <stdin>:1:1: note: scanning from here
# | clang version 21.0.0git (https://github.com/llvm/llvm-project.git cafad2b75a6e1c4957db1967cadeb002339887e3)
# | ^
# | <stdin>:6:1316: note: possible intended match here
# |  "C:\\buildbot\\as-builder-1\\x-armv7l\\build\\bin\\clang.exe" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc" "-emit-llvm-uselists" "-dumpdir" "a-" "-disable-free" "-clear-ast-before-backend" "-main-file-name" "offload-Xarch.c" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=C:\\buildbot\\as-builder-1\\x-armv7l\\build\\tools\\clang\\test\\Driver" "-fcoverage-compilation-dir=C:\\buildbot\\as-builder-1\\x-armv7l\\build\\tools\\clang\\test\\Driver" "-resource-dir" "C:\\buildbot\\as-builder-1\\x-armv7l\\build\\lib\\clang\\21" "-internal-isystem" "C:\\buildbot\\as-builder-1\\x-armv7l\\build\\lib\\clang\\21\\include" "-internal-isystem" "/usr/local/include" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-internal-isystem" "C:\\buildbot\\as-builder-1\\x-armv7l\\build\\lib\\clang\\21\\include" "-internal-isystem" "/usr/local/include" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-ferror-limit" "19" "-fopenmp" "--no-offloadlib" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-disable-llvm-passes" "-fopenmp-targets=amdgcn-amd-amdhsa" "-faddrsig" "-D__GCC_HAVE_DWARF2_CFI_ASM=1" "-o" "C:\\buildbot\\as-builder-1\\x-armv7l\\build\\lit-tmp-z50civbb\\offload-Xarch-415aa2.bc" "-x" "c" "C:\\buildbot\\as-builder-1\\x-armv7l\\llvm-project\\clang\\test\\Driver\\offload-Xarch.c"
# |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ^
# | 
# | Input file: <stdin>
# | Check file: C:\buildbot\as-builder-1\x-armv7l\llvm-project\clang\test\Driver\offload-Xarch.c
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants