Skip to content

[mlir][func] Fix incorrect API usage in FuncOpConversion #113977

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
Oct 29, 2024
Merged
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
14 changes: 9 additions & 5 deletions mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,16 @@ static void wrapExternalFunction(OpBuilder &builder, Location loc,
static void restoreByValRefArgumentType(
ConversionPatternRewriter &rewriter, const LLVMTypeConverter &typeConverter,
ArrayRef<std::optional<NamedAttribute>> byValRefNonPtrAttrs,
LLVM::LLVMFuncOp funcOp) {
ArrayRef<BlockArgument> oldBlockArgs, LLVM::LLVMFuncOp funcOp) {
// Nothing to do for function declarations.
if (funcOp.isExternal())
return;

ConversionPatternRewriter::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(&funcOp.getFunctionBody().front());

for (const auto &[arg, byValRefAttr] :
llvm::zip(funcOp.getArguments(), byValRefNonPtrAttrs)) {
for (const auto &[arg, oldArg, byValRefAttr] :
llvm::zip(funcOp.getArguments(), oldBlockArgs, byValRefNonPtrAttrs)) {
// Skip argument if no `llvm.byval` or `llvm.byref` attribute.
if (!byValRefAttr)
continue;
Expand All @@ -295,7 +295,7 @@ static void restoreByValRefArgumentType(
cast<TypeAttr>(byValRefAttr->getValue()).getValue());

auto valueArg = rewriter.create<LLVM::LoadOp>(arg.getLoc(), resTy, arg);
rewriter.replaceAllUsesExcept(arg, valueArg, valueArg);
rewriter.replaceUsesOfBlockArgument(oldArg, valueArg);
}
}

Expand All @@ -309,6 +309,10 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
return rewriter.notifyMatchFailure(
funcOp, "Only support FunctionOpInterface with FunctionType");

// Keep track of the entry block arguments. They will be needed later.
SmallVector<BlockArgument> oldBlockArgs =
llvm::to_vector(funcOp.getArguments());

// Convert the original function arguments. They are converted using the
// LLVMTypeConverter provided to this legalization pattern.
auto varargsAttr = funcOp->getAttrOfType<BoolAttr>(varargsAttrName);
Expand Down Expand Up @@ -438,7 +442,7 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
// pointee type in the function body when converting `llvm.byval`/`llvm.byref`
// function arguments.
restoreByValRefArgumentType(rewriter, converter, byValRefNonPtrAttrs,
newFuncOp);
oldBlockArgs, newFuncOp);

if (!shouldUseBarePtrCallConv(funcOp, &converter)) {
if (funcOp->getAttrOfType<UnitAttr>(
Expand Down
Loading