Skip to content

llvm-reduce: Fix overly conservative operands-to-args user restriction #133854

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
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
6 changes: 3 additions & 3 deletions llvm/test/tools/llvm-reduce/operands-to-args.ll
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ define void @f(ptr %a) {
@gv_init_use = global [1 x ptr] [ptr @has_global_init_user]

; INTERESTING-LABEL: define void @has_global_init_user(
; REDUCED-LABEL: define void @has_global_init_user() {
; REDUCED-LABEL: define void @has_global_init_user(ptr %Local) {
define void @has_global_init_user() {
%Local = alloca i32, align 4
store i32 42, ptr %Local, align 4
ret void
}

; INTERESTING-LABEL: define void @has_callee_and_arg_user(
; REDUCED-LABEL: define void @has_callee_and_arg_user(ptr %orig.arg) {
; REDUCED-LABEL: define void @has_callee_and_arg_user(ptr %orig.arg, ptr %Local) {
define void @has_callee_and_arg_user(ptr %orig.arg) {
%Local = alloca i32, align 4
store i32 42, ptr %Local, align 4
Expand All @@ -96,7 +96,7 @@ declare void @ptr_user(ptr)

; INTERESTING-LABEL: define void @calls_and_passes_func(
; REDUCED-LABEL: define void @calls_and_passes_func(ptr %has_callee_and_arg_user) {
; REDUCED: call void @has_callee_and_arg_user(ptr %has_callee_and_arg_user)
; REDUCED: call void @has_callee_and_arg_user(ptr %has_callee_and_arg_user, ptr null)
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we pass null here now, should we not ideally introduce a new argument?

Copy link
Contributor Author

@arsenm arsenm Apr 1, 2025

Choose a reason for hiding this comment

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

Whenever this this does anything, the call sites are adjusted to pass the default type value to the new argument

define void @calls_and_passes_func() {
call void @has_callee_and_arg_user(ptr @has_callee_and_arg_user)
ret void
Expand Down
16 changes: 7 additions & 9 deletions llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@

using namespace llvm;

static bool canReplaceFunction(Function *F) {
return all_of(F->uses(), [](Use &Op) {
if (auto *CI = dyn_cast<CallBase>(Op.getUser()))
return &CI->getCalledOperandUse() == &Op;
return false;
});
static bool canReplaceFunction(const Function &F) {
// TODO: Add controls to avoid ABI breaks (e.g. don't break main)
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO llvm-reduce should be reducing main

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shoul, but it would be useful to have a mode to opt out and avoid breaking anything needed to execute the function (bugpoint has a more primitive version of the option than the one I would like)

return true;
}

static bool canReduceUse(Use &Op) {
Expand Down Expand Up @@ -59,8 +56,9 @@ static bool canReduceUse(Use &Op) {
static void replaceFunctionCalls(Function *OldF, Function *NewF) {
SmallVector<CallBase *> Callers;
for (Use &U : OldF->uses()) {
auto *CI = cast<CallBase>(U.getUser());
assert(&U == &CI->getCalledOperandUse());
auto *CI = dyn_cast<CallBase>(U.getUser());
if (!CI || !CI->isCallee(&U)) // RAUW can handle these fine.
continue;

Function *CalledF = CI->getCalledFunction();
if (CalledF == OldF) {
Expand Down Expand Up @@ -209,7 +207,7 @@ void llvm::reduceOperandsToArgsDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) {

SmallVector<Use *> OperandsToReduce;
for (Function &F : make_early_inc_range(Program.functions())) {
if (!canReplaceFunction(&F))
if (!canReplaceFunction(F))
continue;
OperandsToReduce.clear();
for (Instruction &I : instructions(&F)) {
Expand Down