Skip to content

[GlobalOpt] Bail out on non-ConstExprs in isSimpleEnoughtToCommit. #143400

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 3 commits into from
Jun 11, 2025
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
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Utils/Evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ isSimpleEnoughValueToCommitHelper(Constant *C,
// We don't know exactly what relocations are allowed in constant expressions,
// so we allow &global+constantoffset, which is safe and uniformly supported
// across targets.
ConstantExpr *CE = cast<ConstantExpr>(C);
ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
if (!CE)
return false;
switch (CE->getOpcode()) {
case Instruction::BitCast:
// Bitcast is fine if the casted value is fine.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals all --version 5
; RUN: opt -p globalopt -S %s | FileCheck %s

@llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @ctor, ptr null }, { i32, ptr, ptr } { i32 65535, ptr @ctor_nocfi, ptr null }, { i32, ptr, ptr } { i32 65535, ptr @ctor_dso_local_equivalent, ptr null }]

@foo = internal global ptr null

declare void @user(ptr)

;.
; CHECK: @llvm.global_ctors = appending global [3 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @ctor, ptr null }, { i32, ptr, ptr } { i32 65535, ptr @ctor_nocfi, ptr null }, { i32, ptr, ptr } { i32 65535, ptr @ctor_dso_local_equivalent, ptr null }]
; CHECK: @foo = internal global ptr null
;.
define void @ctor() {
; CHECK-LABEL: define void @ctor() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[DST:%.*]] = alloca ptr, align 8
; CHECK-NEXT: store ptr ptrauth (ptr @foo, i32 0), ptr [[DST]], align 8
; CHECK-NEXT: call void @user(ptr [[DST]])
; CHECK-NEXT: ret void
;
entry:
%dst = alloca ptr, align 8
store ptr ptrauth (ptr @foo, i32 0), ptr %dst, align 8
call void @user(ptr %dst)
ret void
}

define void @ctor_nocfi() {
; CHECK-LABEL: define void @ctor_nocfi() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[DST:%.*]] = alloca ptr, align 8
; CHECK-NEXT: store ptr no_cfi @foo, ptr [[DST]], align 8
; CHECK-NEXT: call void @user(ptr [[DST]])
; CHECK-NEXT: ret void
;
entry:
%dst = alloca ptr, align 8
store ptr no_cfi @foo, ptr %dst, align 8
call void @user(ptr %dst)
ret void
}

define void @fn() {
; CHECK-LABEL: define void @fn() {
; CHECK-NEXT: ret void
;
ret void
}

define void @ctor_dso_local_equivalent() {
; CHECK-LABEL: define void @ctor_dso_local_equivalent() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[DST:%.*]] = alloca ptr, align 8
; CHECK-NEXT: store ptr dso_local_equivalent @fn, ptr [[DST]], align 8
; CHECK-NEXT: call void @user(ptr [[DST]])
; CHECK-NEXT: ret void
;
entry:
%dst = alloca ptr, align 8
store ptr dso_local_equivalent @fn, ptr %dst, align 8
call void @user(ptr %dst)
ret void
}
Loading