-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Expand the MemRefToEmitC pass - Adding scalars #148055
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,9 @@ | |
#include "mlir/Dialect/EmitC/IR/EmitC.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
#include "mlir/IR/Builders.h" | ||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/IR/TypeRange.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
using namespace mlir; | ||
|
@@ -83,7 +85,7 @@ struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> { | |
LogicalResult | ||
matchAndRewrite(memref::GlobalOp op, OpAdaptor operands, | ||
ConversionPatternRewriter &rewriter) const override { | ||
|
||
MemRefType type = op.getType(); | ||
if (!op.getType().hasStaticShape()) { | ||
return rewriter.notifyMatchFailure( | ||
op.getLoc(), "cannot transform global with dynamic shape"); | ||
|
@@ -95,7 +97,13 @@ struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> { | |
op.getLoc(), "global variable with alignment requirement is " | ||
"currently not supported"); | ||
} | ||
auto resultTy = getTypeConverter()->convertType(op.getType()); | ||
|
||
Type resultTy; | ||
if (type.getRank() == 0) | ||
resultTy = getTypeConverter()->convertType(type.getElementType()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to just do resultTy = getTypeConverter()->convertType(getElementTypeOrSelf(type)); |
||
else | ||
resultTy = getTypeConverter()->convertType(type); | ||
Comment on lines
+102
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe introduce a helper, since I see a similar pattern in a few spots? |
||
|
||
if (!resultTy) { | ||
return rewriter.notifyMatchFailure(op.getLoc(), | ||
"cannot convert result type"); | ||
|
@@ -114,6 +122,10 @@ struct ConvertGlobal final : public OpConversionPattern<memref::GlobalOp> { | |
bool externSpecifier = !staticSpecifier; | ||
|
||
Attribute initialValue = operands.getInitialValueAttr(); | ||
if (type.getRank() == 0) { | ||
auto elementsAttr = llvm::cast<ElementsAttr>(*op.getInitialValue()); | ||
initialValue = elementsAttr.getSplatValue<Attribute>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the initialValue before this vs splat value returned? |
||
} | ||
if (isa_and_present<UnitAttr>(initialValue)) | ||
initialValue = {}; | ||
|
||
|
@@ -132,7 +144,17 @@ struct ConvertGetGlobal final | |
matchAndRewrite(memref::GetGlobalOp op, OpAdaptor operands, | ||
ConversionPatternRewriter &rewriter) const override { | ||
|
||
auto resultTy = getTypeConverter()->convertType(op.getType()); | ||
MemRefType type = op.getType(); | ||
Type resultTy; | ||
if (type.getRank() == 0) | ||
resultTy = emitc::LValueType::get( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this need to be LValue, while one below not? |
||
getTypeConverter()->convertType(type.getElementType())); | ||
else | ||
resultTy = getTypeConverter()->convertType(type); | ||
|
||
if (!resultTy) | ||
return rewriter.notifyMatchFailure(op.getLoc(), "cannot convert type"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you have this check just below too? |
||
|
||
if (!resultTy) { | ||
return rewriter.notifyMatchFailure(op.getLoc(), | ||
"cannot convert result type"); | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -41,6 +41,8 @@ func.func @memref_load(%buff : memref<4x8xf32>, %i: index, %j: index) -> f32 { | |||
module @globals { | ||||
memref.global "private" constant @internal_global : memref<3x7xf32> = dense<4.0> | ||||
// CHECK-NEXT: emitc.global static const @internal_global : !emitc.array<3x7xf32> = dense<4.000000e+00> | ||||
memref.global "private" constant @__constant_xi32 : memref<i32> = dense<-1> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was expecting to see a corresponding change to
|
||||
// CHECK-NEXT: emitc.global static const @__constant_xi32 : i32 = -1 | ||||
memref.global @public_global : memref<3x7xf32> | ||||
// CHECK-NEXT: emitc.global extern @public_global : !emitc.array<3x7xf32> | ||||
memref.global @uninitialized_global : memref<3x7xf32> = uninitialized | ||||
|
@@ -50,6 +52,8 @@ module @globals { | |||
func.func @use_global() { | ||||
// CHECK-NEXT: emitc.get_global @public_global : !emitc.array<3x7xf32> | ||||
%0 = memref.get_global @public_global : memref<3x7xf32> | ||||
// CHECK- NEXT: emitc.get_global @__constant_xi32 : !emitc.lvalue<i32> | ||||
%1 = memref.get_global @__constant_xi32 : memref<i32> | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to check, so previously |
||||
return | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we want to use
type
as a variable name...