Skip to content

Commit ab68602

Browse files
matthias-springerIanWood1
authored andcommitted
[mlir][LLVM][NFC] Avoid rollback in FuncOp --> LLVM lowering (llvm#136477)
This pattern used to create an `llvm.func` op, then check additional requirements and return "failure". This commit moves the checks before the creation of the replacement op, so that no rollback is necessary when one of the checks fails. Note: This is in preparation of the One-Shot Dialect Conversion refactoring, which removes the rollback functionality.
1 parent 035d50c commit ab68602

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,22 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
320320
// overriden with an LLVM pointer type for later processing.
321321
SmallVector<std::optional<NamedAttribute>> byValRefNonPtrAttrs;
322322
TypeConverter::SignatureConversion result(funcOp.getNumArguments());
323-
auto llvmType = converter.convertFunctionSignature(
324-
funcOp, varargsAttr && varargsAttr.getValue(),
325-
shouldUseBarePtrCallConv(funcOp, &converter), result,
326-
byValRefNonPtrAttrs);
323+
auto llvmType = dyn_cast_or_null<LLVM::LLVMFunctionType>(
324+
converter.convertFunctionSignature(
325+
funcOp, varargsAttr && varargsAttr.getValue(),
326+
shouldUseBarePtrCallConv(funcOp, &converter), result,
327+
byValRefNonPtrAttrs));
327328
if (!llvmType)
328329
return rewriter.notifyMatchFailure(funcOp, "signature conversion failed");
329330

331+
// Check for unsupported variadic functions.
332+
if (!shouldUseBarePtrCallConv(funcOp, &converter))
333+
if (funcOp->getAttrOfType<UnitAttr>(
334+
LLVM::LLVMDialect::getEmitCWrapperAttrName()))
335+
if (llvmType.isVarArg())
336+
return funcOp.emitError("C interface for variadic functions is not "
337+
"supported yet.");
338+
330339
// Create an LLVM function, use external linkage by default until MLIR
331340
// functions have linkage.
332341
LLVM::Linkage linkage = LLVM::Linkage::External;
@@ -342,6 +351,18 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
342351
linkage = attr.getLinkage();
343352
}
344353

354+
// Check for invalid attributes.
355+
StringRef readnoneAttrName = LLVM::LLVMDialect::getReadnoneAttrName();
356+
if (funcOp->hasAttr(readnoneAttrName)) {
357+
auto attr = funcOp->getAttrOfType<UnitAttr>(readnoneAttrName);
358+
if (!attr) {
359+
funcOp->emitError() << "Contains " << readnoneAttrName
360+
<< " attribute not of type UnitAttr";
361+
return rewriter.notifyMatchFailure(
362+
funcOp, "Contains readnone attribute not of type UnitAttr");
363+
}
364+
}
365+
345366
SmallVector<NamedAttribute, 4> attributes;
346367
filterFuncAttributes(funcOp, attributes);
347368
auto newFuncOp = rewriter.create<LLVM::LLVMFuncOp>(
@@ -352,15 +373,7 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
352373
.setVisibility(funcOp.getVisibility());
353374

354375
// Create a memory effect attribute corresponding to readnone.
355-
StringRef readnoneAttrName = LLVM::LLVMDialect::getReadnoneAttrName();
356376
if (funcOp->hasAttr(readnoneAttrName)) {
357-
auto attr = funcOp->getAttrOfType<UnitAttr>(readnoneAttrName);
358-
if (!attr) {
359-
funcOp->emitError() << "Contains " << readnoneAttrName
360-
<< " attribute not of type UnitAttr";
361-
return rewriter.notifyMatchFailure(
362-
funcOp, "Contains readnone attribute not of type UnitAttr");
363-
}
364377
auto memoryAttr = LLVM::MemoryEffectsAttr::get(
365378
rewriter.getContext(),
366379
{LLVM::ModRefInfo::NoModRef, LLVM::ModRefInfo::NoModRef,
@@ -447,10 +460,6 @@ mlir::convertFuncOpToLLVMFuncOp(FunctionOpInterface funcOp,
447460
if (!shouldUseBarePtrCallConv(funcOp, &converter)) {
448461
if (funcOp->getAttrOfType<UnitAttr>(
449462
LLVM::LLVMDialect::getEmitCWrapperAttrName())) {
450-
if (newFuncOp.isVarArg())
451-
return funcOp.emitError("C interface for variadic functions is not "
452-
"supported yet.");
453-
454463
if (newFuncOp.isExternal())
455464
wrapExternalFunction(rewriter, funcOp->getLoc(), converter, funcOp,
456465
newFuncOp);

0 commit comments

Comments
 (0)