This aims to lower `memref.alloc` to `emitc.call_opaque “malloc” ` or
`emitc.call_opaque “aligned_alloc” `
From:
```
module{
func.func @allocating() {
%alloc_5 = memref.alloc() : memref<999xi32>
return
}
}
```
To:
```
module {
emitc.include <"stdlib.h">
func.func @allocating() {
%0 = emitc.call_opaque "sizeof"() {args = [i32]} : () -> !emitc.size_t
%1 = "emitc.constant"() <{value = 999 : index}> : () -> index
%2 = emitc.mul %0, %1 : (!emitc.size_t, index) -> !emitc.size_t
%3 = emitc.call_opaque "malloc"(%2) : (!emitc.size_t) -> !emitc.ptr<!emitc.opaque<"void">>
%4 = emitc.cast %3 : !emitc.ptr<!emitc.opaque<"void">> to !emitc.ptr<i32>
return
}
}
```
Which is then translated as:
```
#include <stdlib.h>
void allocating() {
size_t v1 = sizeof(int32_t);
size_t v2 = 999;
size_t v3 = v1 * v2;
void* v4 = malloc(v3);
int32_t* v5 = (int32_t*) v4;
return;
}
```
90 lines
3.1 KiB
C++
90 lines
3.1 KiB
C++
//===- MemRefToEmitC.cpp - MemRef to EmitC conversion ---------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements a pass to convert memref ops into emitc ops.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Conversion/MemRefToEmitC/MemRefToEmitCPass.h"
|
|
|
|
#include "mlir/Conversion/MemRefToEmitC/MemRefToEmitC.h"
|
|
#include "mlir/Dialect/EmitC/IR/EmitC.h"
|
|
#include "mlir/Dialect/MemRef/IR/MemRef.h"
|
|
#include "mlir/IR/Attributes.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
|
|
namespace mlir {
|
|
#define GEN_PASS_DEF_CONVERTMEMREFTOEMITC
|
|
#include "mlir/Conversion/Passes.h.inc"
|
|
} // namespace mlir
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
struct ConvertMemRefToEmitCPass
|
|
: public impl::ConvertMemRefToEmitCBase<ConvertMemRefToEmitCPass> {
|
|
using Base::Base;
|
|
void runOnOperation() override {
|
|
TypeConverter converter;
|
|
ConvertMemRefToEmitCOptions options;
|
|
options.lowerToCpp = this->lowerToCpp;
|
|
// Fallback for other types.
|
|
converter.addConversion([](Type type) -> std::optional<Type> {
|
|
if (!emitc::isSupportedEmitCType(type))
|
|
return {};
|
|
return type;
|
|
});
|
|
|
|
populateMemRefToEmitCTypeConversion(converter);
|
|
|
|
RewritePatternSet patterns(&getContext());
|
|
populateMemRefToEmitCConversionPatterns(patterns, converter);
|
|
|
|
ConversionTarget target(getContext());
|
|
target.addIllegalDialect<memref::MemRefDialect>();
|
|
target.addLegalDialect<emitc::EmitCDialect>();
|
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
std::move(patterns))))
|
|
return signalPassFailure();
|
|
|
|
mlir::ModuleOp module = getOperation();
|
|
module.walk([&](mlir::emitc::CallOpaqueOp callOp) {
|
|
if (callOp.getCallee() != alignedAllocFunctionName &&
|
|
callOp.getCallee() != mallocFunctionName) {
|
|
return mlir::WalkResult::advance();
|
|
}
|
|
|
|
for (auto &op : *module.getBody()) {
|
|
emitc::IncludeOp includeOp = llvm::dyn_cast<mlir::emitc::IncludeOp>(op);
|
|
if (!includeOp) {
|
|
continue;
|
|
}
|
|
if (includeOp.getIsStandardInclude() &&
|
|
((options.lowerToCpp &&
|
|
includeOp.getInclude() == cppStandardLibraryHeader) ||
|
|
(!options.lowerToCpp &&
|
|
includeOp.getInclude() == cStandardLibraryHeader))) {
|
|
return mlir::WalkResult::interrupt();
|
|
}
|
|
}
|
|
|
|
mlir::OpBuilder builder(module.getBody(), module.getBody()->begin());
|
|
StringAttr includeAttr =
|
|
builder.getStringAttr(options.lowerToCpp ? cppStandardLibraryHeader
|
|
: cStandardLibraryHeader);
|
|
builder.create<mlir::emitc::IncludeOp>(
|
|
module.getLoc(), includeAttr,
|
|
/*is_standard_include=*/builder.getUnitAttr());
|
|
return mlir::WalkResult::interrupt();
|
|
});
|
|
}
|
|
};
|
|
} // namespace
|