Skip to content
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

[FXML-4791] Lower memref expand/collapse to EmitC #313

Merged
merged 36 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
61d5346
Add printout of references with emitc.reference attr
cferry-AMD Aug 28, 2024
1e58520
Support memref expand/collapse
cferry-AMD Aug 26, 2024
3d9b280
Use emitc.reference
cferry-AMD Aug 28, 2024
2257ab4
Add tests, drop CastOp extraClassDeclaration
cferry-AMD Aug 29, 2024
7e6ce4d
Update mlir/lib/Target/Cpp/TranslateToCpp.cpp
cferry-AMD Aug 29, 2024
c387173
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Aug 29, 2024
b90577d
Cast op references
cferry-AMD Aug 29, 2024
450f663
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Aug 29, 2024
d2536c8
Require reference attr on array casting
cferry-AMD Aug 29, 2024
ac9a53e
Emit refs for global ops that have ref attribute
cferry-AMD Aug 29, 2024
f31c9d3
Factor reference attribute name
cferry-AMD Aug 29, 2024
aefc4d7
Remove explicit enumeration, use llvm::enumerate
cferry-AMD Aug 29, 2024
2b4f8ea
Proper Ref attr in Global, Cast ops
cferry-AMD Aug 29, 2024
562a18c
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Sep 2, 2024
cd99673
Exclude dynamic shapes
cferry-AMD Sep 2, 2024
b6dede0
End of files
cferry-AMD Sep 2, 2024
03493b3
Missing end of file
cferry-AMD Sep 2, 2024
adc1c95
Review comments
cferry-AMD Sep 3, 2024
5d9a874
Merge branch 'feature/fused-ops' into corentin.references
cferry-AMD Sep 6, 2024
8d10017
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Sep 6, 2024
0b90451
Add memref.expand_shape output_shape parameter
cferry-AMD Sep 6, 2024
3b8b200
Review comments
cferry-AMD Sep 6, 2024
4eb4a92
Use custom assembly for emitc::FuncOp
cferry-AMD Sep 6, 2024
b442a97
Minor fixes: FuncOp instead of generic, end of files
cferry-AMD Sep 6, 2024
3b3745b
ifdef label + position
cferry-AMD Sep 6, 2024
d117c49
Also test without argument names, fix header signature
cferry-AMD Sep 6, 2024
2137c0f
Review comments: use the ref keyword
cferry-AMD Sep 6, 2024
9a1dc04
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Sep 6, 2024
2e595fc
Remove result list, there is at most one result
cferry-AMD Sep 6, 2024
0dc1c0b
Ensure global refs are initialized
cferry-AMD Sep 9, 2024
63c8ba0
Remove added includes
cferry-AMD Sep 9, 2024
d9839e5
End of file
cferry-AMD Sep 9, 2024
8724dc7
Array-to-array casts without ref still illegal
cferry-AMD Sep 9, 2024
01bda9b
Merge branch 'corentin.references' into corentin.memref_expand_collapse
cferry-AMD Sep 9, 2024
8f9402c
Remove include, no longer needing FunctionOpInterface
cferry-AMD Sep 9, 2024
3fa56ca
Merge remote-tracking branch 'xilinx/feature/fused-ops' into corentin…
cferry-AMD Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ bool isSupportedFloatType(mlir::Type type);
/// Determines whether \p type is a emitc.size_t/ssize_t type.
bool isPointerWideType(mlir::Type type);

/// Give the name of the EmitC reference attribute.
StringRef getReferenceAttributeName();

} // namespace emitc
} // namespace mlir

Expand Down
13 changes: 8 additions & 5 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {

def EmitC_CastOp : EmitC_Op<"cast",
[CExpression,
DeclareOpInterfaceMethods<CastOpInterface>,
SameOperandsAndResultShape]> {
DeclareOpInterfaceMethods<CastOpInterface>]> {
let summary = "Cast operation";
let description = [{
The `cast` operation performs an explicit type conversion and is emitted
Expand All @@ -285,9 +284,11 @@ def EmitC_CastOp : EmitC_Op<"cast",
```
}];

let arguments = (ins EmitCType:$source);
let arguments = (ins EmitCType:$source,
UnitAttr:$reference);
let results = (outs EmitCType:$dest);
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest)";
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest) (`ref` $reference^)?";
let hasVerifier = 1;
}

def EmitC_CmpOp : EmitC_BinaryOp<"cmp", [CExpression]> {
Expand Down Expand Up @@ -1050,14 +1051,16 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
OptionalAttr<EmitC_OpaqueOrTypedAttr>:$initial_value,
UnitAttr:$extern_specifier,
UnitAttr:$static_specifier,
UnitAttr:$const_specifier);
UnitAttr:$const_specifier,
UnitAttr:$reference);

let assemblyFormat = [{
(`extern` $extern_specifier^)?
(`static` $static_specifier^)?
(`const` $const_specifier^)?
$sym_name
`:` custom<EmitCGlobalOpTypeAndInitialValue>($type, $initial_value)
(`ref` $reference^)?
attr-dict
}];

Expand Down
70 changes: 69 additions & 1 deletion mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/Transforms/DialectConversion.h"

Expand Down Expand Up @@ -166,6 +168,71 @@ struct ConvertStore final : public OpConversionPattern<memref::StoreOp> {
return success();
}
};

struct ConvertCollapseShape final
: public OpConversionPattern<memref::CollapseShapeOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(memref::CollapseShapeOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(operands.getSrc());
if (!arrayValue) {
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
}

auto resultTy = getTypeConverter()->convertType(op.getType());
if (!resultTy) {
return rewriter.notifyMatchFailure(op.getLoc(),
"cannot convert result type");
}

// Do not generate casts between arrays with dynamic shapes
auto shape = arrayValue.getType().getShape();
for (auto d : shape) {
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved
if (d == ShapedType::kDynamic)
return failure();
}
auto newCastOp = rewriter.create<emitc::CastOp>(op->getLoc(), resultTy,
operands.getSrc());
newCastOp.setReference(true);
rewriter.replaceOp(op, newCastOp);
return success();
}
};

struct ConvertExpandShape final
: public OpConversionPattern<memref::ExpandShapeOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(memref::ExpandShapeOp op, OpAdaptor operands,
ConversionPatternRewriter &rewriter) const override {
auto arrayValue = dyn_cast<TypedValue<emitc::ArrayType>>(operands.getSrc());
if (!arrayValue) {
return rewriter.notifyMatchFailure(op.getLoc(), "expected array type");
}

auto resultTy = getTypeConverter()->convertType(op.getType());
if (!resultTy) {
return rewriter.notifyMatchFailure(op.getLoc(),
"cannot convert result type");
}

// Do not generate casts between arrays with dynamic shapes
auto shape = arrayValue.getType().getShape();
for (auto d : shape) {
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved
if (d == ShapedType::kDynamic)
return failure();
}
auto newCastOp = rewriter.create<emitc::CastOp>(op->getLoc(), resultTy,
operands.getSrc());
newCastOp.setReference(true);
rewriter.replaceOp(op, newCastOp);
return success();
}
};

} // namespace

void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
Expand All @@ -187,5 +254,6 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
void mlir::populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns,
TypeConverter &converter) {
patterns.add<ConvertAlloca, ConvertGlobal, ConvertGetGlobal, ConvertLoad,
ConvertStore>(converter, patterns.getContext());
ConvertStore, ConvertCollapseShape, ConvertExpandShape>(
converter, patterns.getContext());
}
33 changes: 33 additions & 0 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "mlir/Dialect/EmitC/IR/EmitCTraits.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypeInterfaces.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/IR/IRMapping.h"
Expand Down Expand Up @@ -122,6 +123,8 @@ bool mlir::emitc::isPointerWideType(Type type) {
type);
}

StringRef mlir::emitc::getReferenceAttributeName() { return "emitc.reference"; }

/// Check that the type of the initial value is compatible with the operations
/// result type.
static LogicalResult verifyInitializationAttribute(Operation *op,
Expand Down Expand Up @@ -225,13 +228,39 @@ LogicalResult emitc::AssignOp::verify() {
bool CastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) {
Type input = inputs.front(), output = outputs.front();

// Arrays & pointers don't cast to/from scalars
if (isa<emitc::ArrayType, emitc::PointerType>(input) !=
isa<emitc::ArrayType, emitc::PointerType>(output))
return false;

// Arrays & pointers can be casted to each other.
if (isa<emitc::ArrayType, emitc::PointerType>(input) &&
isa<emitc::ArrayType, emitc::PointerType>(output))
return true;
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved

// Scalars
return (
(emitc::isIntegerIndexOrOpaqueType(input) ||
emitc::isSupportedFloatType(input) || isa<emitc::PointerType>(input)) &&
(emitc::isIntegerIndexOrOpaqueType(output) ||
emitc::isSupportedFloatType(output) || isa<emitc::PointerType>(output)));
}

LogicalResult CastOp::verify() {
bool isReference = this->getReference();

if (isa<emitc::ArrayType>(getDest().getType())) {
if (!isReference)
return emitOpError("cast of array must bear a reference");
return success();
}

if (isReference)
return emitOpError("cast of value type must not bear a reference");

return success();
}

//===----------------------------------------------------------------------===//
// CallOp
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -945,6 +974,10 @@ LogicalResult emitc::ArrayType::verify(
for (int64_t dim : shape) {
if (dim < 0)
return emitError() << "dimensions must have non-negative size";
// TODO support dynamic dimensions on the outermost dimension,
// like int (*a)[50][100]
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved
if (dim == ShapedType::kDynamic)
return emitError() << "dimensions must have static size";
cferry-AMD marked this conversation as resolved.
Show resolved Hide resolved
}

if (!elementType)
Expand Down
Loading