Skip to content

Commit af63dde

Browse files
authored
Reconcile EmitC changes with upstream (#221)
1 parent c328c30 commit af63dde

File tree

20 files changed

+276
-250
lines changed

20 files changed

+276
-250
lines changed

mlir/docs/Dialects/emitc.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ The following convention is followed:
1010
`emitc.call_opaque` operation, C++11 is required.
1111
* If floating-point type template arguments are passed to an `emitc.call_opaque`
1212
operation, C++20 is required.
13+
* If `ssize_t` is used, then the code requires the POSIX header `sys/types.h`
14+
or any of the C++ headers in which the type is defined.
1315
* Else the generated code is compatible with C99.
1416

1517
These restrictions are neither inherent to the EmitC dialect itself nor to the
1618
Cpp emitter and therefore need to be considered while implementing conversions.
1719

20+
Type conversions are provided for the MLIR type `index` into the unsigned `size_t`
21+
type and its signed counterpart `ptrdiff_t`. Conversions between these two types
22+
are only valid if the `index`-typed values are within
23+
`[PTRDIFF_MIN, PTRDIFF_MAX]`.
24+
1825
After the conversion, C/C++ code can be emitted with `mlir-translate`. The tool
1926
supports translating MLIR to C/C++ by passing `-mlir-to-cpp`. Furthermore, code
2027
with variables declared at top can be generated by passing the additional

mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace mlir {
1313
class RewritePatternSet;
1414
class TypeConverter;
1515

16-
void populateArithToEmitCPatterns(RewritePatternSet &patterns,
17-
TypeConverter &typeConverter);
16+
void populateArithToEmitCPatterns(TypeConverter &typeConverter,
17+
RewritePatternSet &patterns);
1818
} // namespace mlir
1919

2020
#endif // MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H

mlir/include/mlir/Dialect/EmitC/IR/EmitC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool isIntegerIndexOrOpaqueType(Type type);
4545
bool isSupportedFloatType(mlir::Type type);
4646

4747
/// Determines whether \p type is a emitc.size_t/ssize_t type.
48-
bool isAnySizeTType(mlir::Type type);
48+
bool isPointerWideType(mlir::Type type);
4949

5050
} // namespace emitc
5151
} // namespace mlir

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
5151
def CExpression : NativeOpTrait<"emitc::CExpression">;
5252

5353
// Types only used in binary arithmetic operations.
54-
def IntegerIndexOrOpaqueType : AnyTypeOf<[EmitCIntegerType, Index,
55-
EmitC_SignedSizeT, EmitC_SizeT, EmitC_OpaqueType]>;
54+
def IntegerIndexOrOpaqueType : Type<CPred<"emitc::isIntegerIndexOrOpaqueType($_self)">,
55+
"integer, index or opaque type supported by EmitC">;
5656
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[EmitCFloatType, IntegerIndexOrOpaqueType]>;
5757

5858
def EmitC_AddOp : EmitC_BinaryOp<"add", [CExpression]> {
@@ -288,7 +288,6 @@ def EmitC_CastOp : EmitC_Op<"cast",
288288
let arguments = (ins EmitCType:$source);
289289
let results = (outs EmitCType:$dest);
290290
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest)";
291-
let hasFolder = 1;
292291
}
293292

294293
def EmitC_CmpOp : EmitC_BinaryOp<"cmp", [CExpression]> {

mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def EmitC_ArrayType : EmitC_Type<"Array", "array", [ShapedTypeInterface]> {
7575
Type elementType) const;
7676

7777
static bool isValidElementType(Type type) {
78-
return type.isIntOrIndexOrFloat() ||
79-
emitc::isAnySizeTType(type) ||
80-
llvm::isa<PointerType, OpaqueType>(type);
78+
return emitc::isSupportedFloatType(type) ||
79+
emitc::isIntegerIndexOrOpaqueType(type) ||
80+
llvm::isa<PointerType>(type);
8181
}
8282
}];
8383
let genVerifyDecl = 1;
@@ -133,10 +133,29 @@ def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {
133133

134134
def EmitC_SignedSizeT : EmitC_Type<"SignedSizeT", "ssize_t"> {
135135
let summary = "EmitC signed size type";
136+
let description = [{
137+
Data type representing all values of `emitc.size_t`, plus -1.
138+
It corresponds to `ssize_t` found in `<sys/types.h>`.
139+
140+
Use of this type causes the code to be non-C99 compliant.
141+
}];
142+
}
143+
144+
def EmitC_PtrDiffT : EmitC_Type<"PtrDiffT", "ptrdiff_t"> {
145+
let summary = "EmitC signed pointer diff type";
146+
let description = [{
147+
Signed data type as wide as platform-specific pointer types.
148+
In particular, it is as wide as `emitc.size_t`.
149+
It corresponds to `ptrdiff_t` found in `<stddef.h>`.
150+
}];
136151
}
137152

138153
def EmitC_SizeT : EmitC_Type<"SizeT", "size_t"> {
139154
let summary = "EmitC unsigned size type";
155+
let description = [{
156+
Unsigned data type as wide as platform-specific pointer types.
157+
It corresponds to `size_t` found in `<stddef.h>`.
158+
}];
140159
}
141160

142161
#endif // MLIR_DIALECT_EMITC_IR_EMITCTYPES

mlir/include/mlir/Dialect/EmitC/Transforms/TypeConversions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
namespace mlir {
1313
class TypeConverter;
14-
void populateEmitCSizeTypeConversions(TypeConverter &converter);
14+
void populateEmitCSizeTTypeConversions(TypeConverter &converter);
15+
1516
} // namespace mlir
1617

1718
#endif // MLIR_DIALECT_EMITC_TRANSFORMS_TYPECONVERSIONS_H

0 commit comments

Comments
 (0)