-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathExport.cpp
More file actions
507 lines (426 loc) · 17.7 KB
/
Export.cpp
File metadata and controls
507 lines (426 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//===-- Export.cpp - Export Substrait dialect to protobuf -------*- C++ -*-===//
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "structured/Target/SubstraitPB/Export.h"
#include "ProtobufUtils.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/Support/LogicalResult.h"
#include "structured/Dialect/Substrait/IR/Substrait.h"
#include "structured/Target/SubstraitPB/Options.h"
#include "llvm/ADT/TypeSwitch.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/json_util.h>
#include <substrait/proto/algebra.pb.h>
#include <substrait/proto/plan.pb.h>
#include <substrait/proto/type.pb.h>
using namespace mlir;
using namespace mlir::substrait;
using namespace ::substrait;
using namespace ::substrait::proto;
namespace pb = google::protobuf;
namespace {
// Forward declaration for the export function of the given operation type.
//
// We need one such function for most op type that we want to export. The
// forward declarations are necessary such all export functions are available
// for the definitions indepedently of the order of these definitions. The
// `MESSAGE_TYPE` argument corresponds to the protobuf message type returned
// by the function.
#define DECLARE_EXPORT_FUNC(OP_TYPE, MESSAGE_TYPE) \
static FailureOr<std::unique_ptr<MESSAGE_TYPE>> exportOperation(OP_TYPE op);
DECLARE_EXPORT_FUNC(CrossOp, Rel)
DECLARE_EXPORT_FUNC(EmitOp, Rel)
DECLARE_EXPORT_FUNC(ExpressionOpInterface, Expression)
DECLARE_EXPORT_FUNC(FieldReferenceOp, Expression)
DECLARE_EXPORT_FUNC(FilterOp, Rel)
DECLARE_EXPORT_FUNC(LiteralOp, Expression)
DECLARE_EXPORT_FUNC(ModuleOp, Plan)
DECLARE_EXPORT_FUNC(NamedTableOp, Rel)
DECLARE_EXPORT_FUNC(PlanOp, Plan)
DECLARE_EXPORT_FUNC(RelOpInterface, Rel)
FailureOr<std::unique_ptr<pb::Message>> exportOperation(Operation *op);
FailureOr<std::unique_ptr<Rel>> exportOperation(RelOpInterface op);
FailureOr<std::unique_ptr<proto::Type>> exportType(Location loc,
mlir::Type mlirType) {
MLIRContext *context = mlirType.getContext();
// Handle SI1.
auto si1 = IntegerType::get(context, 1, IntegerType::Signed);
if (mlirType == si1) {
// TODO(ingomueller): support other nullability modes.
auto i1Type = std::make_unique<proto::Type::Boolean>();
i1Type->set_nullability(
Type_Nullability::Type_Nullability_NULLABILITY_REQUIRED);
auto type = std::make_unique<proto::Type>();
type->set_allocated_bool_(i1Type.release());
return std::move(type);
}
// Handle SI32.
auto si32 = IntegerType::get(context, 32, IntegerType::Signed);
if (mlirType == si32) {
// TODO(ingomueller): support other nullability modes.
auto i32Type = std::make_unique<proto::Type::I32>();
i32Type->set_nullability(
Type_Nullability::Type_Nullability_NULLABILITY_REQUIRED);
auto type = std::make_unique<proto::Type>();
type->set_allocated_i32(i32Type.release());
return std::move(type);
}
if (auto tupleType = llvm::dyn_cast<TupleType>(mlirType)) {
auto structType = std::make_unique<proto::Type::Struct>();
for (mlir::Type fieldType : tupleType.getTypes()) {
// Convert field type recursively.
FailureOr<std::unique_ptr<proto::Type>> type = exportType(loc, fieldType);
if (failed(type))
return failure();
*structType->add_types() = *type.value();
}
auto type = std::make_unique<proto::Type>();
type->set_allocated_struct_(structType.release());
return std::move(type);
}
// TODO(ingomueller): Support other types.
return emitError(loc) << "could not export unsupported type " << mlirType;
}
FailureOr<std::unique_ptr<Rel>> exportOperation(CrossOp op) {
// Build `RelCommon` message.
auto relCommon = std::make_unique<RelCommon>();
auto direct = std::make_unique<RelCommon::Direct>();
relCommon->set_allocated_direct(direct.release());
// Build `left` input message.
auto leftOp =
llvm::dyn_cast_if_present<RelOpInterface>(op.getLeft().getDefiningOp());
if (!leftOp)
return op->emitOpError(
"left input was not produced by Substrait relation op");
FailureOr<std::unique_ptr<Rel>> leftRel = exportOperation(leftOp);
if (failed(leftRel))
return failure();
// Build `right` input message.
auto rightOp =
llvm::dyn_cast_if_present<RelOpInterface>(op.getRight().getDefiningOp());
if (!rightOp)
return op->emitOpError(
"right input was not produced by Substrait relation op");
FailureOr<std::unique_ptr<Rel>> rightRel = exportOperation(rightOp);
if (failed(rightRel))
return failure();
// Build `CrossRel` message.
auto crossRel = std::make_unique<CrossRel>();
crossRel->set_allocated_common(relCommon.release());
crossRel->set_allocated_left(leftRel->release());
crossRel->set_allocated_right(rightRel->release());
// Build `Rel` message.
auto rel = std::make_unique<Rel>();
rel->set_allocated_cross(crossRel.release());
return rel;
}
FailureOr<std::unique_ptr<Rel>> exportOperation(EmitOp op) {
auto inputOp =
dyn_cast_if_present<RelOpInterface>(op.getInput().getDefiningOp());
if (!inputOp)
return op->emitOpError(
"has input that was not produced by Substrait relation op");
// Export input op.
FailureOr<std::unique_ptr<Rel>> inputRel = exportOperation(inputOp);
if (failed(inputRel))
return failure();
// Build the `emit` message.
auto emit = std::make_unique<RelCommon::Emit>();
for (auto intAttr : op.getMapping().getAsRange<IntegerAttr>())
emit->add_output_mapping(intAttr.getInt());
// Attach the `emit` message to the `RelCommon` message.
FailureOr<RelCommon *> relCommon =
protobuf_utils::getMutableCommon(inputRel->get(), op.getLoc());
if (failed(relCommon))
return failure();
if (relCommon.value()->has_emit()) {
InFlightDiagnostic diag =
op->emitOpError("has 'input' that already has 'emit' message "
"(try running canonicalization?)");
diag.attachNote(inputOp.getLoc()) << "op exported to 'input' message";
return diag;
}
relCommon.value()->set_allocated_emit(emit.release());
return inputRel;
}
FailureOr<std::unique_ptr<Expression>>
exportOperation(ExpressionOpInterface op) {
return llvm::TypeSwitch<ExpressionOpInterface,
FailureOr<std::unique_ptr<Expression>>>(op)
.Case<FieldReferenceOp, LiteralOp>([&](auto op) {
llvm::errs() << __PRETTY_FUNCTION__ << "\n";
op.dump();
return exportOperation(op);
})
.Default(
[](auto op) { return op->emitOpError("not supported for export"); });
}
FailureOr<std::unique_ptr<Expression>> exportOperation(FieldReferenceOp op) {
using FieldReference = Expression::FieldReference;
using ReferenceSegment = Expression::ReferenceSegment;
// Build linked list of `ReferenceSegment` messages.
// TODO: support masked references.
std::unique_ptr<Expression::ReferenceSegment> referenceRoot;
for (int64_t pos : llvm::reverse(op.getPosition())) {
// Remember child segment and create new `ReferenceSegment` message.
auto childReference = std::move(referenceRoot);
referenceRoot = std::make_unique<ReferenceSegment>();
// Create `StructField` message.
// TODO(ingomueller): support other segment types.
auto structField = std::make_unique<ReferenceSegment::StructField>();
structField->set_field(pos);
structField->set_allocated_child(childReference.release());
referenceRoot->set_allocated_struct_field(structField.release());
}
// Build `FieldReference` message.
auto fieldReference = std::make_unique<FieldReference>();
fieldReference->set_allocated_direct_reference(referenceRoot.release());
// Handle different `root_type`s.
Value inputVal = op.getContainer();
if (Operation *definingOp = inputVal.getDefiningOp()) {
// If there is a defining op, the `root_type` is an `Expression`.
ExpressionOpInterface exprOp =
llvm::dyn_cast<ExpressionOpInterface>(definingOp);
if (!exprOp)
return op->emitOpError("has 'container' operand that was not produced by "
"Substrait expression");
FailureOr<std::unique_ptr<Expression>> expression = exportOperation(exprOp);
fieldReference->set_allocated_expression(expression->release());
} else {
// Input must be a `BlockArgument`. Only support root references for now.
auto blockArg = llvm::cast<BlockArgument>(inputVal);
if (blockArg.getOwner() != op->getBlock())
// TODO(ingomueller): support outer reference type.
return op.emitOpError("has unsupported outer reference");
auto rootReference = std::make_unique<FieldReference::RootReference>();
fieldReference->set_allocated_root_reference(rootReference.release());
}
// Build `Expression` message.
auto expression = std::make_unique<Expression>();
expression->set_allocated_selection(fieldReference.release());
return expression;
}
FailureOr<std::unique_ptr<Rel>> exportOperation(FilterOp op) {
// Build `RelCommon` message.
auto relCommon = std::make_unique<RelCommon>();
auto direct = std::make_unique<RelCommon::Direct>();
relCommon->set_allocated_direct(direct.release());
// Build input `Rel` message.
auto inputOp =
llvm::dyn_cast_if_present<RelOpInterface>(op.getInput().getDefiningOp());
if (!inputOp)
return op->emitOpError("input was not produced by Substrait relation op");
FailureOr<std::unique_ptr<Rel>> inputRel = exportOperation(inputOp);
if (failed(inputRel))
return failure();
// Build condition `Expression` message.
auto yieldOp = llvm::cast<YieldOp>(op.getCondition().front().getTerminator());
// TODO(ingomueller): There can be cases where there isn't a defining op but
// the region argument is returned directly. Support that.
auto conditionOp = llvm::dyn_cast_if_present<ExpressionOpInterface>(
yieldOp.getValue().getDefiningOp());
if (!conditionOp)
return op->emitOpError("condition not supported for export: yielded op was "
"not produced by Substrait expression op");
FailureOr<std::unique_ptr<Expression>> condition =
exportOperation(conditionOp);
if (failed(condition))
return failure();
// Build `FilterRel` message.
auto filterRel = std::make_unique<FilterRel>();
filterRel->set_allocated_common(relCommon.release());
filterRel->set_allocated_input(inputRel->release());
filterRel->set_allocated_condition(condition->release());
// Build `Rel` message.
auto rel = std::make_unique<Rel>();
rel->set_allocated_filter(filterRel.release());
return rel;
}
FailureOr<std::unique_ptr<Expression>> exportOperation(LiteralOp op) {
// Build `Literal` message depending on type.
auto value = llvm::cast<TypedAttr>(op.getValue());
mlir::Type literalType = value.getType();
auto literal = std::make_unique<Expression::Literal>();
// `IntegerType`s.
if (auto intType = dyn_cast<IntegerType>(literalType)) {
if (!intType.isSigned())
op->emitOpError("has integer value with unsupported signedness");
switch (intType.getWidth()) {
case 1:
literal->set_boolean(value.cast<IntegerAttr>().getSInt());
break;
case 32:
// TODO(ingomueller): Add tests when we can express plans that use i32.
literal->set_i32(value.cast<IntegerAttr>().getSInt());
break;
default:
op->emitOpError("has integer value with unsupported width");
}
} else
op->emitOpError("has unsupported value");
// Build `Expression` message.
auto expression = std::make_unique<Expression>();
expression->set_allocated_literal(literal.release());
return expression;
}
FailureOr<std::unique_ptr<Plan>> exportOperation(ModuleOp op) {
if (!op->getAttrs().empty()) {
op->emitOpError("has attributes");
return failure();
}
Region &body = op.getBodyRegion();
if (llvm::range_size(body.getOps()) != 1) {
op->emitOpError("has more than one op in its body");
return failure();
}
if (auto plan = llvm::dyn_cast<PlanOp>(&*body.op_begin()))
return exportOperation(plan);
op->emitOpError("contains an op that is not a 'substrait.plan'");
return failure();
}
FailureOr<std::unique_ptr<Rel>> exportOperation(NamedTableOp op) {
Location loc = op.getLoc();
// Build `NamedTable` message.
auto namedTable = std::make_unique<ReadRel::NamedTable>();
namedTable->add_names(op.getTableName().getRootReference().str());
for (SymbolRefAttr attr : op.getTableName().getNestedReferences()) {
namedTable->add_names(attr.getLeafReference().str());
}
// Build `RelCommon` message.
auto relCommon = std::make_unique<RelCommon>();
auto direct = std::make_unique<RelCommon::Direct>();
relCommon->set_allocated_direct(direct.release());
// Build `Struct` message.
auto struct_ = std::make_unique<proto::Type::Struct>();
struct_->set_nullability(
Type_Nullability::Type_Nullability_NULLABILITY_REQUIRED);
auto tupleType = llvm::cast<TupleType>(op.getResult().getType());
for (mlir::Type fieldType : tupleType.getTypes()) {
FailureOr<std::unique_ptr<proto::Type>> type = exportType(loc, fieldType);
if (failed(type))
return (failure());
*struct_->add_types() = *std::move(type.value());
}
// Build `NamedStruct` message.
auto namedStruct = std::make_unique<NamedStruct>();
namedStruct->set_allocated_struct_(struct_.release());
for (Attribute attr : op.getFieldNames()) {
namedStruct->add_names(attr.cast<StringAttr>().getValue().str());
}
// Build `ReadRel` message.
auto readRel = std::make_unique<ReadRel>();
readRel->set_allocated_common(relCommon.release());
readRel->set_allocated_base_schema(namedStruct.release());
readRel->set_allocated_named_table(namedTable.release());
// Build `Rel` message.
auto rel = std::make_unique<Rel>();
rel->set_allocated_read(readRel.release());
return rel;
}
FailureOr<std::unique_ptr<Plan>> exportOperation(PlanOp op) {
// Build `Version` message.
auto version = std::make_unique<Version>();
version->set_major_number(op.getMajorNumber());
version->set_minor_number(op.getMinorNumber());
version->set_patch_number(op.getPatchNumber());
version->set_producer(op.getProducer().str());
version->set_git_hash(op.getGitHash().str());
// Build `Plan` message.
auto plan = std::make_unique<Plan>();
plan->set_allocated_version(version.release());
// Add `relation`s to plan.
for (auto relOp : op.getOps<PlanRelOp>()) {
Operation *terminator = relOp.getBody().front().getTerminator();
auto rootOp =
llvm::cast<RelOpInterface>(terminator->getOperand(0).getDefiningOp());
FailureOr<std::unique_ptr<Rel>> rel = exportOperation(rootOp);
if (failed(rel))
return failure();
// Handle `Rel`/`RelRoot` cases depending on whether `names` is set.
PlanRel *planRel = plan->add_relations();
if (std::optional<Attribute> names = relOp.getFieldNames()) {
auto root = std::make_unique<RelRoot>();
root->set_allocated_input(rel->release());
auto namesArray = cast<ArrayAttr>(names.value()).getAsRange<StringAttr>();
for (StringAttr name : namesArray) {
root->add_names(name.getValue().str());
}
planRel->set_allocated_root(root.release());
} else {
planRel->set_allocated_rel(rel->release());
}
}
return std::move(plan);
}
FailureOr<std::unique_ptr<Rel>> exportOperation(RelOpInterface op) {
return llvm::TypeSwitch<Operation *, FailureOr<std::unique_ptr<Rel>>>(op)
.Case<CrossOp, EmitOp, FieldReferenceOp, FilterOp, NamedTableOp>(
[&](auto op) { return exportOperation(op); })
.Default([](auto op) {
op->emitOpError("not supported for export");
return failure();
});
}
FailureOr<std::unique_ptr<pb::Message>> exportOperation(Operation *op) {
return llvm::TypeSwitch<Operation *, FailureOr<std::unique_ptr<pb::Message>>>(
op)
.Case<ModuleOp, PlanOp>(
[&](auto op) -> FailureOr<std::unique_ptr<pb::Message>> {
auto typedMessage = exportOperation(op);
if (failed(typedMessage))
return failure();
return std::unique_ptr<pb::Message>(typedMessage.value().release());
})
.Default([](auto op) {
op->emitOpError("not supported for export");
return failure();
});
}
} // namespace
namespace mlir {
namespace substrait {
LogicalResult
translateSubstraitToProtobuf(Operation *op, llvm::raw_ostream &output,
substrait::ImportExportOptions options) {
FailureOr<std::unique_ptr<pb::Message>> result = exportOperation(op);
if (failed(result))
return failure();
std::string out;
switch (options.serdeFormat) {
case substrait::SerdeFormat::kText:
if (!pb::TextFormat::PrintToString(*result.value(), &out)) {
op->emitOpError("could not be serialized to text format");
return failure();
}
break;
case substrait::SerdeFormat::kBinary:
if (!result->get()->SerializeToString(&out)) {
op->emitOpError("could not be serialized to binary format");
return failure();
}
break;
case substrait::SerdeFormat::kJson:
case substrait::SerdeFormat::kPrettyJson: {
pb::util::JsonOptions jsonOptions;
if (options.serdeFormat == SerdeFormat::kPrettyJson)
jsonOptions.add_whitespace = true;
pb::util::Status status =
pb::util::MessageToJsonString(*result.value(), &out, jsonOptions);
if (!status.ok()) {
InFlightDiagnostic diag =
op->emitOpError("could not be serialized to JSON format");
diag.attachNote() << status.message();
return diag;
}
}
}
output << out;
return success();
}
} // namespace substrait
} // namespace mlir