Skip to content

Commit 2a77d92

Browse files
authored
[clang] Incorrect IR involving the use of bcopy (#79298)
This patch addresses the issue regarding the call of bcopy function in a conditional expression. It is analogous to the already accepted patch which deals with the same problem, just regarding the bzero function [0]. Here is the testcase which illustrates the issue: ``` void bcopy(const void *, void *, unsigned long); void foo(void); void test_bcopy() { char dst[20]; char src[20]; int _sz = 20, len = 20; return (_sz ? ((_sz >= len) ? bcopy(src, dst, len) : foo()) : bcopy(src, dst, len)); } ``` When processing it with clang, following issue occurs: Instruction does not dominate all uses! %arraydecay2 = getelementptr inbounds [20 x i8], ptr %dst, i64 0, i64 0, !dbg !38 %cond = phi ptr [ %arraydecay2, %cond.end ], [ %arraydecay5, %cond.false3 ], !dbg !33 fatal error: error in backend: Broken module found, compilation aborted! This happens because an incorrect phi node is created. It is created because bcopy function call is lowered to the call of llvm.memmove intrinsic and function memmove returns void *. Since llvm.memmove is called in two places in the same return statement, clang creates a phi node in the final basic block for the return value and that phi node is incorrect. However, bcopy function should return void in the first place, so this phi node is unnecessary. This is what this patch addresses. An appropriate test is also added and no existing tests fail when applying this patch. Also, this crash only happens when LLVM is configured with -DLLVM_ENABLE_ASSERTIONS=On option. [0] https://reviews.llvm.org/D39746
1 parent ca654ac commit 2a77d92

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4011,7 +4011,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
40114011
EmitNonNullArgCheck(RValue::get(Dest.getPointer()), E->getArg(1)->getType(),
40124012
E->getArg(1)->getExprLoc(), FD, 0);
40134013
Builder.CreateMemMove(Dest, Src, SizeVal, false);
4014-
return RValue::get(Dest.getPointer());
4014+
return RValue::get(nullptr);
40154015
}
40164016

40174017
case Builtin::BImemcpy:

clang/test/CodeGen/builtins.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,21 @@ void test_conditional_bzero(void) {
202202
// CHECK-NOT: phi
203203
}
204204

205+
// CHECK-LABEL: define{{.*}} void @test_conditional_bcopy
206+
void test_conditional_bcopy(void) {
207+
char dst[20];
208+
char src[20];
209+
int _sz = 20, len = 20;
210+
return (_sz
211+
? ((_sz >= len)
212+
? __builtin_bcopy(src, dst, len)
213+
: foo())
214+
: __builtin_bcopy(src, dst, len));
215+
// CHECK: call void @llvm.memmove
216+
// CHECK: call void @llvm.memmove
217+
// CHECK-NOT: phi
218+
}
219+
205220
// CHECK-LABEL: define{{.*}} void @test_float_builtins
206221
void test_float_builtins(__fp16 *H, float F, double D, long double LD) {
207222
volatile int res;

0 commit comments

Comments
 (0)