forked from OSchip/llvm-project
[mlir][linalg] Add ods-gen helper to simplify the build methods.
Add a helper used to implement the build methods generated by ods-gen. The change reduces code size and compilation time since all structured op builders use the same build method. The change reduces the LinalgOps.cpp compilation time from 10.2s to 9.8s (debug build). Depends On D123987 Reviewed By: nicolasvasilache Differential Revision: https://reviews.llvm.org/D124003
This commit is contained in:
parent
f26c41e8dd
commit
407b351da2
|
@ -82,16 +82,35 @@ static void fillStructuredOpRegion(OpBuilder &opBuilder, Region ®ion,
|
|||
// iterator_types is an auto-generated method.
|
||||
}
|
||||
|
||||
/// Create the region and fill the block of a structured operation given
|
||||
/// `inputTypes` and `outputTypes` as well as a `regionBuilder`.
|
||||
void createAndFillStructuredOpRegion(OpBuilder &opBuilder,
|
||||
OperationState &result,
|
||||
TypeRange inputTypes,
|
||||
TypeRange outputTypes,
|
||||
RegionBuilderFn regionBuilder) {
|
||||
Region ®ion = *result.addRegion();
|
||||
fillStructuredOpRegion(opBuilder, region, inputTypes, outputTypes,
|
||||
result.attributes.getAttrs(), regionBuilder);
|
||||
/// Creates a structured operation given `inputs`, `outputs`, and `attributes`.
|
||||
/// The result types are derived automatically if `resultTensorTypes` is none.
|
||||
/// The body of the operation is filled using `regionBuilder`. All ods-gen
|
||||
/// created structured operations use the method to implement their builders.
|
||||
static void buildStructuredOp(OpBuilder &b, OperationState &state,
|
||||
llvm::Optional<TypeRange> resultTensorTypes,
|
||||
ValueRange inputs, ValueRange outputs,
|
||||
ArrayRef<NamedAttribute> attributes,
|
||||
RegionBuilderFn regionBuilder) {
|
||||
// Derive the result types if needed.
|
||||
SmallVector<Type> derivedResultTypes =
|
||||
resultTensorTypes.getValueOr(TypeRange());
|
||||
if (!resultTensorTypes.hasValue())
|
||||
copy_if(outputs.getTypes(), std::back_inserter(derivedResultTypes),
|
||||
[](Type type) { return type.isa<RankedTensorType>(); });
|
||||
|
||||
state.addOperands(inputs);
|
||||
state.addOperands(outputs);
|
||||
state.addTypes(derivedResultTypes);
|
||||
state.addAttributes(attributes);
|
||||
state.addAttribute(
|
||||
"operand_segment_sizes",
|
||||
b.getI32VectorAttr({static_cast<int32_t>(inputs.size()),
|
||||
static_cast<int32_t>(outputs.size())}));
|
||||
|
||||
// Create and fill the region of the structured operation.
|
||||
Region ®ion = *state.addRegion();
|
||||
fillStructuredOpRegion(b, region, TypeRange(inputs), TypeRange(outputs),
|
||||
state.attributes.getAttrs(), regionBuilder);
|
||||
}
|
||||
|
||||
/// Common parsing used for both named structured ops created by ods-gen and by
|
||||
|
|
|
@ -81,22 +81,8 @@ structured_op: !LinalgStructuredOpConfig
|
|||
# ODS-NEXT: "ValueRange":$outputs, "Attribute":$cast,
|
||||
# ODS-NEXT: CArg<"ArrayRef<NamedAttribute>", "{}">:$attributes),
|
||||
|
||||
# ODS: $_state.addOperands(inputs);
|
||||
# ODS-NEXT: $_state.addOperands(outputs);
|
||||
# ODS-NEXT: $_state.addTypes(resultTensorTypes);
|
||||
# ODS-NEXT: $_state.addAttribute("cast", cast)
|
||||
# ODS-NEXT: $_state.addAttributes(attributes);
|
||||
# ODS-NEXT: $_state.addAttribute(
|
||||
# ODS-NEXT: "operand_segment_sizes",
|
||||
# ODS-NEXT: $_builder.getI32VectorAttr({
|
||||
# ODS-NEXT: static_cast<int32_t>(inputs.size()),
|
||||
# ODS-NEXT: static_cast<int32_t>(outputs.size())}));
|
||||
# ODS-NEXT: createAndFillStructuredOpRegion(
|
||||
# ODS-NEXT: $_builder,
|
||||
# ODS-NEXT: $_state,
|
||||
# ODS-NEXT: TypeRange(inputs),
|
||||
# ODS-NEXT: TypeRange(outputs),
|
||||
# ODS-NEXT: Test1Op::getRegionBuilder()
|
||||
# ODS: buildStructuredOp($_builder, $_state, resultTensorTypes,
|
||||
# ODS-NEXT: attributes, Test1Op::getRegionBuilder())
|
||||
|
||||
|
||||
# IMPL-LABEL: void Test1Op::regionBuilder(ImplicitLocOpBuilder &b,
|
||||
|
|
|
@ -524,46 +524,16 @@ def {0} : LinalgStructuredBase_Op<"{1}", !listconcat([AttrSizedOperandSegments],
|
|||
(ins "ValueRange":$inputs, "ValueRange":$outputs,
|
||||
CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
|
||||
[{{
|
||||
$_state.addOperands(inputs);
|
||||
$_state.addOperands(outputs);
|
||||
SmallVector<Type> resultTensorTypes;
|
||||
copy_if(outputs.getTypes(),
|
||||
std::back_inserter(resultTensorTypes),
|
||||
[](Type type) {{ return type.isa<RankedTensorType>(); });
|
||||
$_state.addTypes(resultTensorTypes);
|
||||
$_state.addAttribute(
|
||||
"operand_segment_sizes",
|
||||
$_builder.getI32VectorAttr({{
|
||||
static_cast<int32_t>(inputs.size()),
|
||||
static_cast<int32_t>(outputs.size())}));
|
||||
$_state.addAttributes(attributes);
|
||||
createAndFillStructuredOpRegion(
|
||||
$_builder,
|
||||
$_state,
|
||||
TypeRange(inputs),
|
||||
TypeRange(outputs),
|
||||
{0}::getRegionBuilder());
|
||||
buildStructuredOp($_builder, $_state, llvm::None, inputs, outputs,
|
||||
attributes, {0}::getRegionBuilder());
|
||||
}]>,
|
||||
OpBuilder<
|
||||
(ins "TypeRange":$resultTensorTypes, "ValueRange":$inputs,
|
||||
"ValueRange":$outputs,
|
||||
CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
|
||||
[{{
|
||||
$_state.addOperands(inputs);
|
||||
$_state.addOperands(outputs);
|
||||
$_state.addTypes(resultTensorTypes);
|
||||
$_state.addAttributes(attributes);
|
||||
$_state.addAttribute(
|
||||
"operand_segment_sizes",
|
||||
$_builder.getI32VectorAttr({{
|
||||
static_cast<int32_t>(inputs.size()),
|
||||
static_cast<int32_t>(outputs.size())}));
|
||||
createAndFillStructuredOpRegion(
|
||||
$_builder,
|
||||
$_state,
|
||||
TypeRange(inputs),
|
||||
TypeRange(outputs),
|
||||
{0}::getRegionBuilder());
|
||||
buildStructuredOp($_builder, $_state, resultTensorTypes,
|
||||
inputs, outputs, attributes, {0}::getRegionBuilder());
|
||||
}]>,
|
||||
OpBuilder<
|
||||
(ins "TypeRange":$resultTensorTypes, "ValueRange":$operands,
|
||||
|
@ -610,22 +580,9 @@ static const char structuredOpBuilderFormat[] = R"FMT(
|
|||
"ValueRange":$outputs, {1},
|
||||
CArg<"ArrayRef<NamedAttribute>", "{{}">:$attributes),
|
||||
[{{
|
||||
$_state.addOperands(inputs);
|
||||
$_state.addOperands(outputs);
|
||||
$_state.addTypes(resultTensorTypes);
|
||||
{2}
|
||||
$_state.addAttributes(attributes);
|
||||
$_state.addAttribute(
|
||||
"operand_segment_sizes",
|
||||
$_builder.getI32VectorAttr({{
|
||||
static_cast<int32_t>(inputs.size()),
|
||||
static_cast<int32_t>(outputs.size())}));
|
||||
createAndFillStructuredOpRegion(
|
||||
$_builder,
|
||||
$_state,
|
||||
TypeRange(inputs),
|
||||
TypeRange(outputs),
|
||||
{0}::getRegionBuilder());
|
||||
buildStructuredOp($_builder, $_state, resultTensorTypes, inputs, outputs,
|
||||
attributes, {0}::getRegionBuilder());
|
||||
}]>
|
||||
)FMT";
|
||||
|
||||
|
|
Loading…
Reference in New Issue