Skip to content

[EmitC] Add ArrayType (#26) #114

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

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 50 additions & 2 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,64 @@

include "mlir/IR/AttrTypeBase.td"
include "mlir/Dialect/EmitC/IR/EmitCBase.td"
include "mlir/IR/BuiltinTypeInterfaces.td"

//===----------------------------------------------------------------------===//
// EmitC type definitions
//===----------------------------------------------------------------------===//

class EmitC_Type<string name, string typeMnemonic>
: TypeDef<EmitC_Dialect, name> {
class EmitC_Type<string name, string typeMnemonic, list<Trait> traits = []>
: TypeDef<EmitC_Dialect, name, traits> {
let mnemonic = typeMnemonic;
}

def EmitC_ArrayType : EmitC_Type<"Array", "array", [ShapedTypeInterface]> {
let summary = "EmitC array type";

let description = [{
An array data type.

Example:

```mlir
// Array emitted as `int32_t[10]`
!emitc.array<10xi32>
// Array emitted as `float[10][20]`
!emitc.ptr<10x20xf32>
```
}];

let parameters = (ins
ArrayRefParameter<"int64_t">:$shape,
"Type":$elementType
);

let builders = [
TypeBuilderWithInferredContext<(ins
"ArrayRef<int64_t>":$shape,
"Type":$elementType
), [{
return $_get(elementType.getContext(), shape, elementType);
}]>
];
let extraClassDeclaration = [{
/// Returns if this type is ranked (always true).
bool hasRank() const { return true; }

/// Clone this vector type with the given shape and element type. If the
/// provided shape is `std::nullopt`, the current shape of the type is used.
ArrayType cloneWith(std::optional<ArrayRef<int64_t>> shape,
Type elementType) const;

static bool isValidElementType(Type type) {
return type.isIntOrIndexOrFloat() ||
llvm::isa<PointerType, OpaqueType>(type);
}
}];
let genVerifyDecl = 1;
let hasCustomAssemblyFormat = 1;
}

def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
let summary = "EmitC opaque type";

Expand Down
63 changes: 63 additions & 0 deletions mlir/lib/Dialect/EmitC/IR/EmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,69 @@ void emitc::OpaqueAttr::print(AsmPrinter &printer) const {
#define GET_TYPEDEF_CLASSES
#include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"

//===----------------------------------------------------------------------===//
// ArrayType
//===----------------------------------------------------------------------===//

Type emitc::ArrayType::parse(AsmParser &parser) {
if (parser.parseLess())
return Type();

SmallVector<int64_t, 4> dimensions;
if (parser.parseDimensionList(dimensions, /*allowDynamic=*/false,
/*withTrailingX=*/true))
return Type();
// Parse the element type.
auto typeLoc = parser.getCurrentLocation();
Type elementType;
if (parser.parseType(elementType))
return Type();

// Check that memref is formed from allowed types.
if (!isValidElementType(elementType))
return parser.emitError(typeLoc, "invalid array element type"), Type();
if (parser.parseGreater())
return Type();
return parser.getChecked<ArrayType>(dimensions, elementType);
}

void emitc::ArrayType::print(AsmPrinter &printer) const {
printer << "<";
for (int64_t dim : getShape()) {
printer << dim << 'x';
}
printer.printType(getElementType());
printer << ">";
}

LogicalResult emitc::ArrayType::verify(
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
::llvm::ArrayRef<int64_t> shape, Type elementType) {
if (shape.empty())
return emitError() << "shape must not be empty";

for (auto d : shape) {
if (d <= 0)
return emitError() << "dimensions must have positive size";
}

if (!elementType)
return emitError() << "element type must not be none";

if (!isValidElementType(elementType))
return emitError() << "invalid array element type";

return success();
}

emitc::ArrayType
emitc::ArrayType::cloneWith(std::optional<ArrayRef<int64_t>> shape,
Type elementType) const {
if (!shape)
return emitc::ArrayType::get(getShape(), elementType);
return emitc::ArrayType::get(*shape, elementType);
}

//===----------------------------------------------------------------------===//
// OpaqueType
//===----------------------------------------------------------------------===//
Expand Down
54 changes: 54 additions & 0 deletions mlir/test/Dialect/EmitC/invalid_types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,57 @@ func.func @illegal_opaque_type_2() {
// expected-error @+1 {{pointer not allowed as outer type with !emitc.opaque, use !emitc.ptr instead}}
%1 = "emitc.variable"(){value = "nullptr" : !emitc.opaque<"int32_t*">} : () -> !emitc.opaque<"int32_t*">
}

// -----

func.func @illegal_array_missing_spec(
// expected-error @+1 {{expected non-function type}}
%arg0: !emitc.array<>) {
}

// -----

func.func @illegal_array_missing_shape(
// expected-error @+1 {{shape must not be empty}}
%arg9: !emitc.array<i32>) {
}

// -----

func.func @illegal_array_missing_x(
// expected-error @+1 {{expected 'x' in dimension list}}
%arg0: !emitc.array<10>
) {
}

// -----

func.func @illegal_array_non_positive_dimenson(
// expected-error @+1 {{dimensions must have positive size}}
%arg0: !emitc.array<0xi32>
) {
}

// -----

func.func @illegal_array_missing_type(
// expected-error @+1 {{expected non-function type}}
%arg0: !emitc.array<10x>
) {
}

// -----

func.func @illegal_array_dynamic_shape(
// expected-error @+1 {{expected static shape}}
%arg0: !emitc.array<10x?xi32>
) {
}

// -----

func.func @illegal_array_unranked(
// expected-error @+1 {{expected non-function type}}
%arg0: !emitc.array<*xi32>
) {
}
14 changes: 14 additions & 0 deletions mlir/test/Dialect/EmitC/types.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ func.func @pointer_types() {

return
}

// CHECK-LABEL: func @array_types(
func.func @array_types(
// CHECK-SAME: !emitc.array<1xf32>,
%arg0: !emitc.array<1xf32>,
// CHECK-SAME: !emitc.array<10x20x30xi32>,
%arg1: !emitc.array<10x20x30xi32>,
// CHECK-SAME: !emitc.array<30x!emitc.ptr<i32>>,
%arg2: !emitc.array<30x!emitc.ptr<i32>>,
// CHECK-SAME: !emitc.array<30x!emitc.opaque<"int">>
%arg3: !emitc.array<30x!emitc.opaque<"int">>
) {
return
}