Skip to content

[LLVM][ConstantFold] Undefined values are not constant #130713

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 1 commit into from
Mar 12, 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
2 changes: 2 additions & 0 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ Constant *Constant::mergeUndefsWith(Constant *C, Constant *Other) {
}

bool Constant::isManifestConstant() const {
if (isa<UndefValue>(this))
return false;
if (isa<ConstantData>(this))
return true;
if (isa<ConstantAggregate>(this) || isa<ConstantExpr>(this)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@ define i1 @global_array() {
%1 = call i1 @llvm.is.constant.i64(i64 ptrtoint (ptr @real_mode_blob_end to i64))
ret i1 %1
}

;; Ensure that is.constant of undef gets lowered reasonably to "false" in
;; optimized codegen: specifically that the "true" branch is eliminated.
;; CHECK-NOT: tail call i32 @subfun_1()
;; CHECK: tail call i32 @subfun_2()
;; CHECK-NOT: tail call i32 @subfun_1()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
;; CHECK-NOT: tail call i32 @subfun_1()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my understanding of this CHECK pattern at the top of the file (where I copied this test style from) the second CHECK-NOT is to make sure there is no re-ordering happening: i.e. it must be fully missing, not just present later.

define i32 @test_undef_branch() nounwind {
%v = call i1 @llvm.is.constant.i32(i32 undef)
br i1 %v, label %True, label %False

True:
%call1 = tail call i32 @subfun_1()
ret i32 %call1

False:
%call2 = tail call i32 @subfun_2()
ret i32 %call2
}
Loading