Skip to content

Commit d7724c0

Browse files
committed
It works.
1 parent 9b401b7 commit d7724c0

10 files changed

Lines changed: 310 additions & 137 deletions

File tree

compiler/include/garel/GARelAttr.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def AggregateFunc : I64EnumAttr<
3838
I64EnumAttrCase<"MAX", 2>,
3939
I64EnumAttrCase<"LOR", 3>, /* Logical OR (over i1) */
4040
I64EnumAttrCase<"ARGMIN", 4>,
41+
I64EnumAttrCase<"COUNT", 5>,
4142
]
4243
> {
4344
let cppNamespace = "::garel";
@@ -49,7 +50,7 @@ def Aggregator : GARel_Attr<"Aggregator", "aggregator"> {
4950

5051
let parameters = (ins
5152
"AggregateFunc":$func,
52-
ArrayRefParameter<"ColumnIdx">:$inputs);
53+
OptionalArrayRefParameter<"ColumnIdx">:$inputs);
5354

5455
let assemblyFormat = [{
5556
`<` $func $inputs `>`

compiler/include/garel/GARelOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def ForOp : GARel_Op<"for", [InferTypeOpAdaptor]> {
143143

144144
let arguments = (ins
145145
Variadic<Relation>:$init,
146-
I64Attr:$iters,
146+
I64Relation:$iters,
147147
I64Attr:$resultIdx);
148148

149149
let regions = (region

compiler/include/garel/GARelTypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <mlir/IR/MLIRContext.h>
34
#include <mlir/IR/Types.h>
45

56
#include "garel/GARelAttr.h"
@@ -11,4 +12,6 @@ namespace garel {
1112

1213
bool isColumnType(mlir::Type t);
1314

15+
RelationType getI64RelationType(mlir::MLIRContext *ctx);
16+
1417
} // namespace garel

compiler/include/garel/GARelTypes.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ def Tuple : GARel_Type<"Tuple", "tuple"> {
3434

3535
def ColumnType : Type<CPred<"::garel::isColumnType($_self)">, "column type">;
3636

37+
def I64Relation : Type<
38+
CPred<"::garel::getI64RelationType($_self.getContext()) == $_self">,
39+
"relation with a single i64 column",
40+
"RelationType">,
41+
BuildableType<"::garel::getI64RelationType($_builder.getContext())">;
42+
3743
#endif // GAREL_TYPES

compiler/src/garel/GARelAttr.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@ mlir::Type AggregatorAttr::getResultType(mlir::Type inputRel) {
2424
case AggregateFunc::ARGMIN:
2525
// NOTE: argmin(arg, val) also uses first input column as output type.
2626
return llvm::cast<RelationType>(inputRel).getColumns()[getInputs()[0]];
27+
case AggregateFunc::COUNT:
28+
return mlir::IntegerType::get(inputRel.getContext(), 64);
29+
}
30+
}
31+
32+
static std::size_t expectedNumInputs(AggregateFunc f) {
33+
switch (f) {
34+
case AggregateFunc::SUM:
35+
case AggregateFunc::MIN:
36+
case AggregateFunc::MAX:
37+
case AggregateFunc::LOR:
38+
return 1;
39+
case AggregateFunc::ARGMIN:
40+
return 2;
41+
case AggregateFunc::COUNT:
42+
return 0;
2743
}
2844
}
2945

3046
mlir::LogicalResult
3147
AggregatorAttr::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
3248
AggregateFunc func, llvm::ArrayRef<ColumnIdx> inputs) {
33-
if (func == AggregateFunc::ARGMIN) {
34-
if (inputs.size() != 2) {
35-
return emitError() << stringifyAggregateFunc(func)
36-
<< " expects exactly two inputs (arg, val), got "
37-
<< inputs.size();
38-
}
39-
} else {
40-
if (inputs.size() != 1) {
41-
return emitError() << stringifyAggregateFunc(func)
42-
<< " expects exactly one input, got " << inputs.size();
43-
}
49+
if (inputs.size() != expectedNumInputs(func)) {
50+
return emitError() << stringifyAggregateFunc(func) << " expects exactly "
51+
<< expectedNumInputs(func) << " inputs, got "
52+
<< inputs.size();
4453
}
4554

4655
return mlir::success();

compiler/src/garel/GARelTypes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <llvm/ADT/ArrayRef.h>
22
#include <llvm/ADT/TypeSwitch.h>
33
#include <mlir/IR/Builders.h>
4+
#include <mlir/IR/BuiltinTypes.h>
45
#include <mlir/IR/DialectImplementation.h>
56
#include <mlir/IR/OpImplementation.h>
67

@@ -18,6 +19,11 @@ bool isColumnType(mlir::Type t) {
1819
t.isIndex();
1920
}
2021

22+
RelationType getI64RelationType(mlir::MLIRContext *ctx) {
23+
return RelationType::get(
24+
ctx, mlir::ArrayRef<mlir::Type>{mlir::IntegerType::get(ctx, 64)});
25+
}
26+
2127
// Need to define this here to avoid depending on IPRTypes in
2228
// IPRDialect and creating a cycle.
2329
void GARelDialect::registerTypes() {

0 commit comments

Comments
 (0)