Skip to content
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

[mlir][llvm] Do not inline variadic functions #77241

Merged
merged 1 commit into from Jan 8, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp
Expand Up @@ -663,6 +663,10 @@ struct LLVMInlinerInterface : public DialectInlinerInterface {
<< "Cannot inline: callable is not an LLVM::LLVMFuncOp\n");
return false;
}
if (funcOp.isVarArg()) {
LLVM_DEBUG(llvm::dbgs() << "Cannot inline: callable is variadic\n");
return false;
}
// TODO: Generate aliasing metadata from noalias argument/result attributes.
if (auto attrs = funcOp.getArgAttrs()) {
for (DictionaryAttr attrDict : attrs->getAsRange<DictionaryAttr>()) {
Expand Down Expand Up @@ -704,7 +708,8 @@ struct LLVMInlinerInterface : public DialectInlinerInterface {
}

bool isLegalToInline(Operation *op, Region *, bool, IRMapping &) const final {
return true;
// The inliner cannot handle variadic function arguments.
return !isa<LLVM::VaStartOp>(op);
}

/// Handle the given inlined return by replacing it with a branch. This
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/LLVMIR/inlining.mlir
Expand Up @@ -644,3 +644,28 @@ llvm.func @caller(%ptr : !llvm.ptr) -> i32 {
llvm.store %c5, %ptr { access_groups = [#caller] } : i32, !llvm.ptr
llvm.return %0 : i32
}

// -----

llvm.func @vararg_func(...) {
llvm.return
}

llvm.func @vararg_intrinrics() {
%0 = llvm.mlir.constant(1 : i32) : i32
%list = llvm.alloca %0 x !llvm.struct<"struct.va_list_opaque", (ptr)> : (i32) -> !llvm.ptr
// The vararg intinriscs should normally be part of a variadic function.
// However, this test uses a non-variadic function to ensure the presence of
// the intrinsic alone suffices to prevent inlining.
llvm.intr.vastart %list : !llvm.ptr
llvm.return
}

// CHECK-LABEL: func @caller
llvm.func @caller() {
// CHECK-NEXT: llvm.call @vararg_func()
llvm.call @vararg_func() vararg(!llvm.func<void (...)>) : () -> ()
// CHECK-NEXT: llvm.call @vararg_intrinrics()
llvm.call @vararg_intrinrics() : () -> ()
llvm.return
}