[PATCH] D101847: [AMDGPU] Fix function pointer argument bug in AMDGPU Propagate Attributes pass.
Jacob Weightman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 4 10:52:13 PDT 2021
jweightman created this revision.
Herald added subscribers: jdoerfert, kerbowa, hiraditya, t-tye, tpr, dstuttard, yaxunl, nhaehnle, jvesely, kzhuravl, arsenm.
jweightman requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.
This patch fixes a bug in the AMDGPU Propagate Attributes pass where a call
instruction with a function pointer argument is identified as a user of the
passed function, and illegally replaces the called function of the
instruction with the function argument.
For example, given functions f and g with appropriate types, the following
illegal transformation could occur without this fix:
call void @f(void ()* @g)
-->
call void @g(void ()* @g.1)
The solution introduced in this patch is to prevent the cloning and
substitution if the instruction's called function and the function which
might be cloned do not match.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D101847
Files:
llvm/lib/Target/AMDGPU/AMDGPUPropagateAttributes.cpp
llvm/test/CodeGen/AMDGPU/propagate-attributes-function-pointer-argument.ll
Index: llvm/test/CodeGen/AMDGPU/propagate-attributes-function-pointer-argument.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/AMDGPU/propagate-attributes-function-pointer-argument.ll
@@ -0,0 +1,32 @@
+; This is a regression test for a bug in the AMDGPU Propagate Attributes pass
+; where a call instruction's callee could be replaced with a function pointer
+; passed to the original call instruction as an argument.
+;
+; Example:
+; `call void @f(void ()* @g)`
+; could become
+; `call void @g(void ()* @g.1)`
+; which is invalid IR.
+
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -amdgpu-propagate-attributes-late %s | FileCheck %s
+
+; CHECK: define amdgpu_kernel void @thiswasabug() #0
+; CHECK: call void @f(void ()* @g)
+define amdgpu_kernel void @thiswasabug() #0 {
+ call void @f(void ()* @g)
+ ret void
+}
+
+define private void @f(void ()* nocapture %0) #0 {
+ ret void
+}
+
+; In order to expose this bug, it is necessary that `g` have one of the
+; propagated attributes, so that a clone and substitution would take place if g
+; were actually the function being called.
+define private void @g() #1 {
+ ret void
+}
+
+attributes #0 = { noinline }
+attributes #1 = { noinline "amdgpu-waves-per-eu"="1,10" }
Index: llvm/lib/Target/AMDGPU/AMDGPUPropagateAttributes.cpp
===================================================================
--- llvm/lib/Target/AMDGPU/AMDGPUPropagateAttributes.cpp
+++ llvm/lib/Target/AMDGPU/AMDGPUPropagateAttributes.cpp
@@ -249,7 +249,7 @@
if (!I)
continue;
CallBase *CI = dyn_cast<CallBase>(I);
- if (!CI)
+ if (!CI || CI->getCalledFunction() != &F)
continue;
Function *Caller = CI->getCaller();
if (!Caller || !Visited.insert(CI).second)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101847.342801.patch
Type: text/x-patch
Size: 1823 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210504/ef6592cc/attachment.bin>
More information about the llvm-commits
mailing list