From 8304ab5799b4172462877ca7495115c659ec0be0 Mon Sep 17 00:00:00 2001 From: Alex Zinenko Date: Fri, 26 Jun 2020 14:47:38 +0200 Subject: [PATCH] [mlir] Avoid creating local OpBuilders in Standard-to-LLVM conversion Conversions of allocation-related operations in Standard-to-LLVM need declarations of "malloc" and "free" (or equivalents). They use locally created OpBuilders pointed at the module level to declare these functions if necessary. This is poorly compatible with the pattern infrastructure that is unaware of new operations being created. Update the insertion point of the main rewriter instead. Differential Revision: https://reviews.llvm.org/D82649 --- mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp index 9376d53..2a3c7a3 100644 --- a/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp +++ b/mlir/lib/Conversion/StandardToLLVM/StandardToLLVM.cpp @@ -1785,12 +1785,14 @@ struct AllocLikeOpLowering : public ConvertOpToLLVMPattern { auto module = allocOp.getParentOfType(); auto allocFunc = module.lookupSymbol(allocFuncName); if (!allocFunc) { - OpBuilder moduleBuilder(op->getParentOfType().getBodyRegion()); + OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToStart( + op->getParentOfType().getBody()); SmallVector callArgTypes = {getIndexType()}; // aligned_alloc(size_t alignment, size_t size) if (useAlignedAlloc) callArgTypes.push_back(getIndexType()); - allocFunc = moduleBuilder.create( + allocFunc = rewriter.create( rewriter.getUnknownLoc(), allocFuncName, LLVM::LLVMType::getFunctionTy(getVoidPtrType(), callArgTypes, /*isVarArg=*/false)); @@ -2093,8 +2095,10 @@ struct DeallocOpLowering : public ConvertOpToLLVMPattern { auto freeFunc = op->getParentOfType().lookupSymbol("free"); if (!freeFunc) { - OpBuilder moduleBuilder(op->getParentOfType().getBodyRegion()); - freeFunc = moduleBuilder.create( + OpBuilder::InsertionGuard guard(rewriter); + rewriter.setInsertionPointToStart( + op->getParentOfType().getBody()); + freeFunc = rewriter.create( rewriter.getUnknownLoc(), "free", LLVM::LLVMType::getFunctionTy(getVoidType(), getVoidPtrType(), /*isVarArg=*/false)); -- 2.7.4