Skip to content
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

[GlobalOpt][Evaluator] Don't evaluate calls with signature mismatch #119548

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions llvm/include/llvm/Transforms/Utils/Evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@ class Evaluator {
ValueStack.back()[V] = C;
}

/// Casts call result to a type of bitcast call expression
Constant *castCallResultIfNeeded(Type *ReturnType, Constant *RV);

/// Given call site return callee and list of its formal arguments
Function *getCalleeWithFormalArgs(CallBase &CB,
SmallVectorImpl<Constant *> &Formals);
Expand Down
40 changes: 6 additions & 34 deletions llvm/lib/Transforms/Utils/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,40 +253,17 @@ Evaluator::getCalleeWithFormalArgs(CallBase &CB,

bool Evaluator::getFormalParams(CallBase &CB, Function *F,
SmallVectorImpl<Constant *> &Formals) {
if (!F)
return false;

auto *FTy = F->getFunctionType();
if (FTy->getNumParams() > CB.arg_size()) {
LLVM_DEBUG(dbgs() << "Too few arguments for function.\n");
if (FTy != CB.getFunctionType()) {
LLVM_DEBUG(dbgs() << "Signature mismatch.\n");
return false;
}

auto ArgI = CB.arg_begin();
for (Type *PTy : FTy->params()) {
auto *ArgC = ConstantFoldLoadThroughBitcast(getVal(*ArgI), PTy, DL);
if (!ArgC) {
LLVM_DEBUG(dbgs() << "Can not convert function argument.\n");
return false;
}
Formals.push_back(ArgC);
++ArgI;
}
for (Value *Arg : CB.args())
Formals.push_back(getVal(Arg));
return true;
}

/// If call expression contains bitcast then we may need to cast
/// evaluated return value to a type of the call expression.
Constant *Evaluator::castCallResultIfNeeded(Type *ReturnType, Constant *RV) {
if (!RV || RV->getType() == ReturnType)
return RV;

RV = ConstantFoldLoadThroughBitcast(RV, ReturnType, DL);
if (!RV)
LLVM_DEBUG(dbgs() << "Failed to fold bitcast call expr\n");
return RV;
}

/// Evaluate all instructions in block BB, returning true if successful, false
/// if we can't evaluate it. NewBB returns the next BB that control flows into,
/// or null upon return. StrippedPointerCastsForAliasAnalysis is set to true if
Expand Down Expand Up @@ -520,9 +497,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
if (Callee->isDeclaration()) {
// If this is a function we can constant fold, do it.
if (Constant *C = ConstantFoldCall(&CB, Callee, Formals, TLI)) {
InstResult = castCallResultIfNeeded(CB.getType(), C);
if (!InstResult)
return false;
InstResult = C;
LLVM_DEBUG(dbgs() << "Constant folded function call. Result: "
<< *InstResult << "\n");
} else {
Expand All @@ -544,10 +519,7 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB,
return false;
}
ValueStack.pop_back();
InstResult = castCallResultIfNeeded(CB.getType(), RetVal);
if (RetVal && !InstResult)
return false;

InstResult = RetVal;
if (InstResult) {
LLVM_DEBUG(dbgs() << "Successfully evaluated function. Result: "
<< *InstResult << "\n\n");
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/GlobalOpt/evaluate-call-errors.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
; REQUIRES: asserts
; RUN: opt -passes=globalopt,instcombine -S -debug-only=evaluator %s -o %t 2>&1 | FileCheck %s

; CHECK: Failed to fold bitcast call expr
; CHECK: Can not convert function argument
; CHECK: Signature mismatch.

target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.12.0"
Expand Down
11 changes: 4 additions & 7 deletions llvm/test/Transforms/GlobalOpt/evaluate-constfold-call.ll
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
; Check if we can evaluate a bitcasted call to a function which is constant folded.
; Evaluator folds call to fmodf, replacing it with constant value in case both operands
; are known at compile time.
; Check that we do not try to evaluate function calls with signature
; mismatches.
; RUN: opt -passes=globalopt,instcombine %s -S -o - | FileCheck %s

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

source_filename = "main.cpp"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
Expand Down
26 changes: 26 additions & 0 deletions llvm/test/Transforms/GlobalOpt/evaluate-ret-void-mismatch.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=globalopt < %s | FileCheck %s

; Don't evaluate call with return value type mismatch.

@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @__cxx_global_var_init, ptr null }]

define void @__cxa_guard_acquire() {
; CHECK-LABEL: define void @__cxa_guard_acquire() local_unnamed_addr {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: ret void
;
entry:
ret void
}

define void @__cxx_global_var_init() {
; CHECK-LABEL: define void @__cxx_global_var_init() {
; CHECK-NEXT: [[RES:%.*]] = call i32 @__cxa_guard_acquire()
; CHECK-NEXT: [[TOBOOL_NOT:%.*]] = icmp eq i32 [[RES]], 0
; CHECK-NEXT: ret void
;
%res = call i32 @__cxa_guard_acquire()
%tobool.not = icmp eq i32 %res, 0
ret void
}
Loading