@@ -230,6 +230,14 @@ static const char *const opCommentHeader = R"(
230
230
231
231
)" ;
232
232
233
+ static const char *const inlineCreateBody = R"(
234
+ OperationState __state__(loc, getOperationName());
235
+ build(builder, __state__, {0});
236
+ auto __res__ = dyn_cast<{1}>(builder.create(__state__));
237
+ assert(__res__ && "builder didn't return the right type");
238
+ return __res__;
239
+ )" ;
240
+
233
241
// ===----------------------------------------------------------------------===//
234
242
// Utility structs and functions
235
243
// ===----------------------------------------------------------------------===//
@@ -665,6 +673,7 @@ class OpEmitter {
665
673
// Generates the build() method that takes each operand/attribute
666
674
// as a stand-alone parameter.
667
675
void genSeparateArgParamBuilder ();
676
+ void genInlineCreateBody (const SmallVector<MethodParameter> ¶mList);
668
677
669
678
// Generates the build() method that takes each operand/attribute as a
670
679
// stand-alone parameter. The generated build() method uses first operand's
@@ -2557,6 +2566,36 @@ static bool canInferType(const Operator &op) {
2557
2566
return op.getTrait (" ::mlir::InferTypeOpInterface::Trait" );
2558
2567
}
2559
2568
2569
+ void OpEmitter::genInlineCreateBody (
2570
+ const SmallVector<MethodParameter> ¶mList) {
2571
+ SmallVector<MethodParameter> createParamList;
2572
+ SmallVector<llvm::StringRef, 4 > nonBuilderStateArgsList;
2573
+ createParamList.emplace_back (" ::mlir::OpBuilder &" , " builder" );
2574
+ std::string locParamName = " loc" ;
2575
+ while (llvm::find_if (paramList, [&locParamName](const MethodParameter &p) {
2576
+ return p.getName () == locParamName;
2577
+ })) {
2578
+ locParamName += " _" ;
2579
+ }
2580
+ createParamList.emplace_back (" ::mlir::Location" , locParamName);
2581
+
2582
+ for (auto ¶m : paramList) {
2583
+ if (param.getType () == " ::mlir::OpBuilder &" or
2584
+ param.getType () == " ::mlir::OperationState &" )
2585
+ continue ;
2586
+ createParamList.emplace_back (param.getType (), param.getName (),
2587
+ param.getDefaultValue (), param.isOptional ());
2588
+ nonBuilderStateArgsList.push_back (param.getName ());
2589
+ }
2590
+ auto *c = opClass.addStaticMethod (opClass.getClassName (), " create" ,
2591
+ createParamList);
2592
+ std::string nonBuilderStateArgs = " " ;
2593
+ llvm::raw_string_ostream nonBuilderStateArgsOS (nonBuilderStateArgs);
2594
+ interleaveComma (nonBuilderStateArgsList, nonBuilderStateArgsOS);
2595
+ c->body () << llvm::formatv (inlineCreateBody, nonBuilderStateArgs,
2596
+ opClass.getClassName ());
2597
+ }
2598
+
2560
2599
void OpEmitter::genSeparateArgParamBuilder () {
2561
2600
SmallVector<AttrParamKind, 2 > attrBuilderType;
2562
2601
attrBuilderType.push_back (AttrParamKind::WrappedAttr);
@@ -2573,10 +2612,12 @@ void OpEmitter::genSeparateArgParamBuilder() {
2573
2612
buildParamList (paramList, inferredAttributes, resultNames, paramKind,
2574
2613
attrType);
2575
2614
2576
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
2615
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
2577
2616
// If the builder is redundant, skip generating the method.
2578
2617
if (!m)
2579
2618
return ;
2619
+ genInlineCreateBody (paramList);
2620
+
2580
2621
auto &body = m->body ();
2581
2622
genCodeForAddingArgAndRegionForBuilder (body, inferredAttributes,
2582
2623
/* isRawValueAttr=*/ attrType ==
@@ -2701,10 +2742,11 @@ void OpEmitter::genUseOperandAsResultTypeCollectiveParamBuilder(
2701
2742
if (op.getNumVariadicRegions ())
2702
2743
paramList.emplace_back (" unsigned" , " numRegions" );
2703
2744
2704
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
2745
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
2705
2746
// If the builder is redundant, skip generating the method
2706
2747
if (!m)
2707
2748
return ;
2749
+ genInlineCreateBody (paramList);
2708
2750
auto &body = m->body ();
2709
2751
2710
2752
// Operands
@@ -2815,10 +2857,11 @@ void OpEmitter::genInferredTypeCollectiveParamBuilder(
2815
2857
if (op.getNumVariadicRegions ())
2816
2858
paramList.emplace_back (" unsigned" , " numRegions" );
2817
2859
2818
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
2860
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
2819
2861
// If the builder is redundant, skip generating the method
2820
2862
if (!m)
2821
2863
return ;
2864
+ genInlineCreateBody (paramList);
2822
2865
auto &body = m->body ();
2823
2866
2824
2867
int numResults = op.getNumResults ();
@@ -2895,10 +2938,11 @@ void OpEmitter::genUseOperandAsResultTypeSeparateParamBuilder() {
2895
2938
buildParamList (paramList, inferredAttributes, resultNames,
2896
2939
TypeParamKind::None, attrType);
2897
2940
2898
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
2941
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
2899
2942
// If the builder is redundant, skip generating the method
2900
2943
if (!m)
2901
2944
return ;
2945
+ genInlineCreateBody (paramList);
2902
2946
auto &body = m->body ();
2903
2947
genCodeForAddingArgAndRegionForBuilder (body, inferredAttributes,
2904
2948
/* isRawValueAttr=*/ attrType ==
@@ -2937,10 +2981,11 @@ void OpEmitter::genUseAttrAsResultTypeCollectiveParamBuilder(
2937
2981
: " attributes" ;
2938
2982
paramList.emplace_back (" ::llvm::ArrayRef<::mlir::NamedAttribute>" ,
2939
2983
attributesName, " {}" );
2940
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
2984
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
2941
2985
// If the builder is redundant, skip generating the method
2942
2986
if (!m)
2943
2987
return ;
2988
+ genInlineCreateBody (paramList);
2944
2989
2945
2990
auto &body = m->body ();
2946
2991
@@ -3103,10 +3148,11 @@ void OpEmitter::genCollectiveParamBuilder(CollectiveBuilderKind kind) {
3103
3148
if (op.getNumVariadicRegions ())
3104
3149
paramList.emplace_back (" unsigned" , " numRegions" );
3105
3150
3106
- auto *m = opClass.addStaticMethod (" void" , " build" , std::move ( paramList) );
3151
+ auto *m = opClass.addStaticMethod (" void" , " build" , paramList);
3107
3152
// If the builder is redundant, skip generating the method
3108
3153
if (!m)
3109
3154
return ;
3155
+ genInlineCreateBody (paramList);
3110
3156
auto &body = m->body ();
3111
3157
3112
3158
// Operands
0 commit comments