Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions ir/attrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class FnAttrs final {
void add(AllocKind k) { allockind |= (uint8_t)k; }
bool has(AllocKind k) const { return allockind & (uint8_t)k; }
bool isAlloc() const { return allockind != 0 || has(AllocSize); }
bool isAllocWithoutSize() const { return allockind != 0 && !has(AllocSize); }

void inferImpliedAttributes();

Expand Down
18 changes: 17 additions & 1 deletion ir/instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2684,7 +2684,23 @@ StateValue FnCall::toSMT(State &s) const {
if (attrs.has(AllocKind::Alloc) ||
attrs.has(AllocKind::Realloc) ||
attrs.has(FnAttrs::AllocSize)) {
Comment thread
nunoplopes marked this conversation as resolved.
auto [size, np_size] = attrs.computeAllocSize(s, args);

smt::expr size;
smt::expr np_size;

if (!attrs.has(FnAttrs::AllocSize)) {
static IntType sizet_type(string("size_t"), config::max_sizet_bits);
auto result = s.addFnCall(std::move(fnName_mangled).str(), std::move(inputs),
std::move(ptr_inputs), sizet_type, StateValue{},
nullptr, std::vector<StateValue>{}, attrs, indirect_hash);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we need to adjust the attrs here so the call is read-only.

@IamYJLee IamYJLee Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

FnAttrs readonly_attrs = attrs;
readonly_attrs.mem.setCanOnlyRead();
auto result = s.addFnCall(std::move(fnName_mangled).str(), std::move(inputs),
      std::move(ptr_inputs), sizet_type, StateValue{},
      nullptr, std::vector<StateValue>{}, readonly_attrs, indirect_hash);

I attempted the change as above, yet the error ERROR: Source and target don't have the same return domain persists.
I think this may be due to the existence of counterexamples in which the myalloc function fails to return.
Would it make sense to add the willreturn attribute to explicitly state that the user-defined allocator always returns (possibly with any value)?

@IamYJLee IamYJLee Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The semantics of this behavior are not explicitly specified in the LLVM Language Reference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Uhm, that's a good point about willreturn. LLVM currently removes calls to custom allocators, though.
Would you mind opening either a discussion on discourse or a bug report to clarify the assumptions on custom allocators, (and CC me) please? Thanks!

size = std::move(result.value);
np_size = std::move(result.non_poison);
} else {
auto result = attrs.computeAllocSize(s, args);
size = std::move(result.first);
np_size = std::move(result.second);
}

expr nonnull = attrs.isNonNull() ? expr(true)
: expr::mkBoolVar("malloc_never_fails");
// FIXME: alloc-family below
Expand Down
14 changes: 14 additions & 0 deletions tests/alive-tv/attrs/noallocsize-fn.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; ERROR: Source and target don't have the same return domain

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the wrong output. A call into an allocator can be removed.


define i64 @src() {
%stack = call ptr @myalloc()
%sz = call i64 @llvm.objectsize.i64.p0(ptr %stack, i1 false, i1 false, i1 false)
ret i64 %sz
}

define i64 @tgt() {
ret i64 -1
}

declare ptr @myalloc() allockind("alloc")
declare i64 @llvm.objectsize.i64.p0(ptr, i1, i1, i1)
3 changes: 3 additions & 0 deletions tools/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,9 @@ static void calculateAndInitConstants(Transform &t) {
inaccessiblememonly_fns.emplace(call->getName()).second)
++num_inaccessiblememonly_fns;
}
if(attrs.isAllocWithoutSize()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

missing space here before (

has_write_fncall |= attrs.mem.canWriteSomething();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think we want the call to get the object size to write anything.

@IamYJLee IamYJLee Jan 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will adjust the arguments to addFnCall to make the call read-only, which enables the removal of this code.

}
if (call->isIndirect()) {
has_indirect_fncalls = true;
num_inaccessiblememonly_fns += is_src;
Expand Down
Loading