[llvm] 7fed2b1 - [llubi] Add support for freeze (#185718)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 11 08:04:07 PDT 2026
Author: Yingwei Zheng
Date: 2026-03-11T23:04:01+08:00
New Revision: 7fed2b1faeb674fdf62dfec1e07a687c3e211271
URL: https://github.com/llvm/llvm-project/commit/7fed2b1faeb674fdf62dfec1e07a687c3e211271
DIFF: https://github.com/llvm/llvm-project/commit/7fed2b1faeb674fdf62dfec1e07a687c3e211271.diff
LOG: [llubi] Add support for freeze (#185718)
The main logic is implemented in `Context` as the RNG is not exposed
outside the `Context` class.
Added:
llvm/test/tools/llubi/freeze.ll
Modified:
llvm/tools/llubi/lib/Context.cpp
llvm/tools/llubi/lib/Context.h
llvm/tools/llubi/lib/Interpreter.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llubi/freeze.ll b/llvm/test/tools/llubi/freeze.ll
new file mode 100644
index 0000000000000..cc79a8d897f4c
--- /dev/null
+++ b/llvm/test/tools/llubi/freeze.ll
@@ -0,0 +1,34 @@
+; NOTE: Assertions have been autogenerated by utils/update_llubi_test_checks.py UTC_ARGS: --version 6
+; RUN: llubi --verbose < %s 2>&1 | FileCheck %s
+
+define void @main() {
+ %int_freeze = freeze i32 10
+ %int_freeze_poison1 = freeze i32 poison
+ %int_freeze_poison2 = freeze i32 poison
+
+ %float_freeze = freeze float 1.0
+ %float_freeze_poison = freeze float poison
+
+ %ptr_freeze = freeze ptr null
+ %ptr_freeze_poison = freeze ptr poison
+
+ %vec_freeze = freeze <2 x i32> <i32 10, i32 poison>
+ %arr_freeze = freeze [2 x i32] [i32 10, i32 poison]
+ %struct_freeze = freeze { i32, float } { i32 10, float poison }
+ %struct_freeze_nested = freeze { i32, { float, ptr } } { i32 poison, { float, ptr } { float 10.0, ptr poison } }
+ ret void
+}
+; CHECK: Entering function: main
+; CHECK-NEXT: %int_freeze = freeze i32 10 => i32 10
+; CHECK-NEXT: %int_freeze_poison1 = freeze i32 poison => i32 -884876226
+; CHECK-NEXT: %int_freeze_poison2 = freeze i32 poison => i32 1044445579
+; CHECK-NEXT: %float_freeze = freeze float 1.000000e+00 => float 1.000000e+00
+; CHECK-NEXT: %float_freeze_poison = freeze float poison => float 0xF032E8B9
+; CHECK-NEXT: %ptr_freeze = freeze ptr null => ptr 0x0 [dangling]
+; CHECK-NEXT: %ptr_freeze_poison = freeze ptr poison => ptr 0x98F56903CEE3FCEE [dangling]
+; CHECK-NEXT: %vec_freeze = freeze <2 x i32> <i32 10, i32 poison> => { i32 10, i32 990324140 }
+; CHECK-NEXT: %arr_freeze = freeze [2 x i32] [i32 10, i32 poison] => { i32 10, i32 -44045842 }
+; CHECK-NEXT: %struct_freeze = freeze { i32, float } { i32 10, float poison } => { i32 10, float 0x042AE12F }
+; CHECK-NEXT: %struct_freeze_nested = freeze { i32, { float, ptr } } { i32 poison, { float, ptr } { float 1.000000e+01, ptr poison } } => { i32 1166165736, { float 1.000000e+01, ptr 0xD79E62976F604366 [dangling] } }
+; CHECK-NEXT: ret void
+; CHECK-NEXT: Exiting function: main
diff --git a/llvm/tools/llubi/lib/Context.cpp b/llvm/tools/llubi/lib/Context.cpp
index f91a890c03e55..968ac4f561558 100644
--- a/llvm/tools/llubi/lib/Context.cpp
+++ b/llvm/tools/llubi/lib/Context.cpp
@@ -375,6 +375,50 @@ void Context::storeRawBytes(MemoryObject &MO, uint64_t Offset, const void *Data,
MO[Offset + I] = Byte::concrete(static_cast<const uint8_t *>(Data)[I]);
}
+void Context::freeze(AnyValue &Val, Type *Ty) {
+ if (Val.isPoison()) {
+ uint32_t Bits = DL.getTypeSizeInBits(Ty);
+ APInt RandomVal = APInt::getZero(Bits);
+ if (UndefBehavior == UndefValueBehavior::NonDeterministic) {
+ SmallVector<APInt::WordType> RandomWords;
+ uint32_t NumWords = APInt::getNumWords(Bits);
+ RandomWords.reserve(NumWords);
+ static_assert(decltype(Rng)::word_size >=
+ std::numeric_limits<APInt::WordType>::digits,
+ "Unexpected Rng result type.");
+ for (uint32_t I = 0; I != NumWords; ++I)
+ RandomWords.push_back(static_cast<APInt::WordType>(Rng()));
+ RandomVal = APInt(Bits, RandomWords);
+ }
+ if (Ty->isIntegerTy())
+ Val = AnyValue(RandomVal);
+ else if (Ty->isFloatingPointTy())
+ Val = AnyValue(APFloat(Ty->getFltSemantics(), RandomVal));
+ else if (Ty->isPointerTy())
+ Val = AnyValue(Pointer(RandomVal));
+ else
+ llvm_unreachable("Unsupported scalar type for poison value");
+ return;
+ }
+ if (Val.isAggregate()) {
+ auto &SubVals = Val.asAggregate();
+ if (auto *VecTy = dyn_cast<VectorType>(Ty)) {
+ Type *ElemTy = VecTy->getElementType();
+ for (auto &SubVal : SubVals)
+ freeze(SubVal, ElemTy);
+ } else if (auto *ArrTy = dyn_cast<ArrayType>(Ty)) {
+ Type *ElemTy = ArrTy->getElementType();
+ for (auto &SubVal : SubVals)
+ freeze(SubVal, ElemTy);
+ } else if (auto *StructTy = dyn_cast<StructType>(Ty)) {
+ for (uint32_t I = 0, E = SubVals.size(); I != E; ++I)
+ freeze(SubVals[I], StructTy->getElementType(I));
+ } else {
+ llvm_unreachable("Invalid aggregate type");
+ }
+ }
+}
+
MemoryObject::~MemoryObject() = default;
MemoryObject::MemoryObject(uint64_t Addr, uint64_t Size, StringRef Name,
unsigned AS, MemInitKind InitKind)
diff --git a/llvm/tools/llubi/lib/Context.h b/llvm/tools/llubi/lib/Context.h
index 8d9c0dfbd2434..d1960b270d9bd 100644
--- a/llvm/tools/llubi/lib/Context.h
+++ b/llvm/tools/llubi/lib/Context.h
@@ -245,6 +245,9 @@ class Context {
void storeRawBytes(MemoryObject &MO, uint64_t Offset, const void *Data,
uint64_t Size);
+ /// Freeze the value in-place.
+ void freeze(AnyValue &Val, Type *Ty);
+
Function *getTargetFunction(const Pointer &Ptr);
BasicBlock *getTargetBlock(const Pointer &Ptr);
diff --git a/llvm/tools/llubi/lib/Interpreter.cpp b/llvm/tools/llubi/lib/Interpreter.cpp
index 948fb3334423a..3876036abc01b 100644
--- a/llvm/tools/llubi/lib/Interpreter.cpp
+++ b/llvm/tools/llubi/lib/Interpreter.cpp
@@ -1119,6 +1119,12 @@ class InstExecutor : public InstVisitor<InstExecutor, void> {
setResult(BCI, Ctx.fromBytes(Bytes, BCI.getType()));
}
+ void visitFreezeInst(FreezeInst &FI) {
+ AnyValue Val = getValue(FI.getOperand(0));
+ Ctx.freeze(Val, FI.getType());
+ setResult(FI, std::move(Val));
+ }
+
/// This function implements the main interpreter loop.
/// It handles function calls in a non-recursive manner to avoid stack
/// overflows.
More information about the llvm-commits
mailing list