[Mlir-commits] [mlir] EmitC: Add emitc.global and emitc.get_global (#145) (PR #88701)
Simon Camphausen
llvmlistbot at llvm.org
Mon Apr 22 06:21:24 PDT 2024
================
@@ -50,6 +50,68 @@ struct ConvertAlloca final : public OpConversionPattern<memref::AllocaOp> {
}
};
+struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> {
+ using OpConversionPattern::OpConversionPattern;
+
+ LogicalResult
+ matchAndRewrite(memref::GlobalOp op, OpAdaptor operands,
+ ConversionPatternRewriter &rewriter) const override {
+
+ if (!op.getType().hasStaticShape()) {
+ return rewriter.notifyMatchFailure(
+ op.getLoc(), "cannot transform global with dynamic shape");
+ }
+
+ if (op.getAlignment().value_or(1) > 1) {
+ // TODO: Extend GlobalOp to specify alignment via the `alignas` specifier.
+ return rewriter.notifyMatchFailure(
+ op.getLoc(), "global variable with alignment requirement is "
+ "currently not supported");
+ }
+ auto resultTy = getTypeConverter()->convertType(op.getType());
+ if (!resultTy) {
+ return rewriter.notifyMatchFailure(op.getLoc(),
+ "cannot convert result type");
+ }
+
+ SymbolTable::Visibility visibility = SymbolTable::getSymbolVisibility(op);
+ if (visibility != SymbolTable::Visibility::Public &&
+ visibility != SymbolTable::Visibility::Private) {
+ return rewriter.notifyMatchFailure(
+ op.getLoc(),
+ "only public and private visibility is currently supported");
+ }
+ // We are explicit in specifier the linkage because the default linkage
+ // for constants is different in C and C++.
+ bool staticSpecifier = visibility == SymbolTable::Visibility::Private;
+ bool externSpecifier = !staticSpecifier;
+
+ rewriter.replaceOpWithNewOp<emitc::GlobalOp>(
+ op, operands.getSymName(), resultTy, operands.getInitialValueAttr(),
----------------
simon-camp wrote:
We need to explicitly handle uninitialized globals. (By mapping UnitAttr to std::nullopt I think)
https://github.com/llvm/llvm-project/pull/88701
More information about the Mlir-commits
mailing list