Skip to content

Commit 5013c81

Browse files
authored
[GlobalOpt][Evaluator] Don't evaluate calls with signature mismatch (#119548)
The global ctor evaluator tries to evalute function calls where the call function type and function type do not match, by performing bitcasts. This currently causes a crash when calling a void function with non-void return type. I've opted to remove this functionality entirely rather than fixing this specific case. With opaque pointers, there shouldn't be a legitimate use case for this anymore, as we don't need to look through pointer type casts. Doing other bitcasts is very iffy because it ignores ABI considerations. We should at least leave adjusting the signatures to make them line up to InstCombine (which also does some iffy things, but is at least somewhat more constrained). Fixes #118725.
1 parent e335290 commit 5013c81

File tree

5 files changed

+37
-46
lines changed

5 files changed

+37
-46
lines changed

llvm/include/llvm/Transforms/Utils/Evaluator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ class Evaluator {
125125
ValueStack.back()[V] = C;
126126
}
127127

128-
/// Casts call result to a type of bitcast call expression
129-
Constant *castCallResultIfNeeded(Type *ReturnType, Constant *RV);
130-
131128
/// Given call site return callee and list of its formal arguments
132129
Function *getCalleeWithFormalArgs(CallBase &CB,
133130
SmallVectorImpl<Constant *> &Formals);

llvm/lib/Transforms/Utils/Evaluator.cpp

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -253,40 +253,17 @@ Evaluator::getCalleeWithFormalArgs(CallBase &CB,
253253

254254
bool Evaluator::getFormalParams(CallBase &CB, Function *F,
255255
SmallVectorImpl<Constant *> &Formals) {
256-
if (!F)
257-
return false;
258-
259256
auto *FTy = F->getFunctionType();
260-
if (FTy->getNumParams() > CB.arg_size()) {
261-
LLVM_DEBUG(dbgs() << "Too few arguments for function.\n");
257+
if (FTy != CB.getFunctionType()) {
258+
LLVM_DEBUG(dbgs() << "Signature mismatch.\n");
262259
return false;
263260
}
264261

265-
auto ArgI = CB.arg_begin();
266-
for (Type *PTy : FTy->params()) {
267-
auto *ArgC = ConstantFoldLoadThroughBitcast(getVal(*ArgI), PTy, DL);
268-
if (!ArgC) {
269-
LLVM_DEBUG(dbgs() << "Can not convert function argument.\n");
270-
return false;
271-
}
272-
Formals.push_back(ArgC);
273-
++ArgI;
274-
}
262+
for (Value *Arg : CB.args())
263+
Formals.push_back(getVal(Arg));
275264
return true;
276265
}
277266

278-
/// If call expression contains bitcast then we may need to cast
279-
/// evaluated return value to a type of the call expression.
280-
Constant *Evaluator::castCallResultIfNeeded(Type *ReturnType, Constant *RV) {
281-
if (!RV || RV->getType() == ReturnType)
282-
return RV;
283-
284-
RV = ConstantFoldLoadThroughBitcast(RV, ReturnType, DL);
285-
if (!RV)
286-
LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
287-
return RV;
288-
}
289-
290267
/// Evaluate all instructions in block BB, returning true if successful, false
291268
/// if we can't evaluate it. NewBB returns the next BB that control flows into,
292269
/// or null upon return. StrippedPointerCastsForAliasAnalysis is set to true if
@@ -520,9 +497,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
520497
if (Callee->isDeclaration()) {
521498
// If this is a function we can constant fold, do it.
522499
if (Constant *C = ConstantFoldCall(&CB, Callee, Formals, TLI)) {
523-
InstResult = castCallResultIfNeeded(CB.getType(), C);
524-
if (!InstResult)
525-
return false;
500+
InstResult = C;
526501
LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
527502
<< *InstResult << "\n");
528503
} else {
@@ -544,10 +519,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
544519
return false;
545520
}
546521
ValueStack.pop_back();
547-
InstResult = castCallResultIfNeeded(CB.getType(), RetVal);
548-
if (RetVal && !InstResult)
549-
return false;
550-
522+
InstResult = RetVal;
551523
if (InstResult) {
552524
LLVM_DEBUG(dbgs() << "Successfully evaluated function. Result: "
553525
<< *InstResult << "\n\n");

llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
; REQUIRES: asserts
44
; RUN: opt -passes=globalopt,instcombine -S -debug-only=evaluator %s -o %t 2>&1 | FileCheck %s
55

6-
; CHECK: Failed to fold bitcast call expr
7-
; CHECK: Can not convert function argument
6+
; CHECK: Signature mismatch.
87

98
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
109
target triple = "x86_64-apple-macosx10.12.0"

llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
; Check if we can evaluate a bitcasted call to a function which is constant folded.
2-
; Evaluator folds call to fmodf, replacing it with constant value in case both operands
3-
; are known at compile time.
1+
; Check that we do not try to evaluate function calls with signature
2+
; mismatches.
43
; RUN: opt -passes=globalopt,instcombine %s -S -o - | FileCheck %s
54

6-
; CHECK: @_q = dso_local local_unnamed_addr global %struct.Q { i32 1066527622 }
7-
; CHECK: define dso_local i32 @main
8-
; CHECK-NEXT: %[[V:.+]] = load i32, ptr @_q
9-
; CHECK-NEXT: ret i32 %[[V]]
5+
; CHECK: @_q = dso_local global %struct.Q zeroinitializer
6+
; CHECK: @llvm.global_ctors
107

118
source_filename = "main.cpp"
129
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -S -passes=globalopt < %s | FileCheck %s
3+
4+
; Don't evaluate call with return value type mismatch.
5+
6+
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr null }]
7+
8+
define void @__cxa_guard_acquire() {
9+
; CHECK-LABEL: define void @__cxa_guard_acquire() local_unnamed_addr {
10+
; CHECK-NEXT: [[ENTRY:.*:]]
11+
; CHECK-NEXT: ret void
12+
;
13+
entry:
14+
ret void
15+
}
16+
17+
define void @__cxx_global_var_init() {
18+
; CHECK-LABEL: define void @__cxx_global_var_init() {
19+
; CHECK-NEXT: [[RES:%.*]] = call i32 @__cxa_guard_acquire()
20+
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[RES]], 0
21+
; CHECK-NEXT: ret void
22+
;
23+
%res = call i32 @__cxa_guard_acquire()
24+
%tobool.not = icmp eq i32 %res, 0
25+
ret void
26+
}

0 commit comments

Comments
 (0)