@@ -92,6 +92,46 @@ Operation *cir::CIRDialect::materializeConstant(mlir::OpBuilder &builder,
9292// Helpers
9393// ===----------------------------------------------------------------------===//
9494
95+ // Parses one of the keywords provided in the list `keywords` and returns the
96+ // position of the parsed keyword in the list. If none of the keywords from the
97+ // list is parsed, returns -1.
98+ static int parseOptionalKeywordAlternative (AsmParser &parser,
99+ ArrayRef<llvm::StringRef> keywords) {
100+ for (auto en : llvm::enumerate (keywords)) {
101+ if (succeeded (parser.parseOptionalKeyword (en.value ())))
102+ return en.index ();
103+ }
104+ return -1 ;
105+ }
106+
107+ namespace {
108+ template <typename Ty> struct EnumTraits {};
109+
110+ #define REGISTER_ENUM_TYPE (Ty ) \
111+ template <> struct EnumTraits <cir::Ty> { \
112+ static llvm::StringRef stringify (cir::Ty value) { \
113+ return stringify##Ty (value); \
114+ } \
115+ static unsigned getMaxEnumVal () { return cir::getMaxEnumValFor##Ty (); } \
116+ }
117+
118+ REGISTER_ENUM_TYPE (SideEffect);
119+ } // namespace
120+
121+ // / Parse an enum from the keyword, return failure if the keyword is not found.
122+ template <typename EnumTy, typename RetTy = EnumTy>
123+ static ParseResult parseCIRKeyword (AsmParser &parser, RetTy &result) {
124+ llvm::SmallVector<llvm::StringRef, 10 > names;
125+ for (unsigned i = 0 , e = EnumTraits<EnumTy>::getMaxEnumVal (); i <= e; ++i)
126+ names.push_back (EnumTraits<EnumTy>::stringify (static_cast <EnumTy>(i)));
127+
128+ int index = parseOptionalKeywordAlternative (parser, names);
129+ if (index == -1 )
130+ return failure ();
131+ result = static_cast <RetTy>(index);
132+ return success ();
133+ }
134+
95135// Check if a region's termination omission is valid and, if so, creates and
96136// inserts the omitted terminator into the region.
97137static LogicalResult ensureRegionTerm (OpAsmParser &parser, Region ®ion,
@@ -534,6 +574,18 @@ static mlir::ParseResult parseCallCommon(mlir::OpAsmParser &parser,
534574 if (parser.parseRParen ())
535575 return mlir::failure ();
536576
577+ if (parser.parseOptionalKeyword (" side_effect" ).succeeded ()) {
578+ if (parser.parseLParen ().failed ())
579+ return failure ();
580+ cir::SideEffect sideEffect;
581+ if (parseCIRKeyword<cir::SideEffect>(parser, sideEffect).failed ())
582+ return failure ();
583+ if (parser.parseRParen ().failed ())
584+ return failure ();
585+ auto attr = cir::SideEffectAttr::get (parser.getContext (), sideEffect);
586+ result.addAttribute (" side_effect" , attr);
587+ }
588+
537589 if (parser.parseOptionalAttrDict (result.attributes ))
538590 return ::mlir::failure ();
539591
@@ -556,7 +608,8 @@ static mlir::ParseResult parseCallCommon(mlir::OpAsmParser &parser,
556608static void printCallCommon (mlir::Operation *op,
557609 mlir::FlatSymbolRefAttr calleeSym,
558610 mlir::Value indirectCallee,
559- mlir::OpAsmPrinter &printer) {
611+ mlir::OpAsmPrinter &printer,
612+ cir::SideEffect sideEffect) {
560613 printer << ' ' ;
561614
562615 auto callLikeOp = mlir::cast<cir::CIRCallOpInterface>(op);
@@ -572,7 +625,13 @@ static void printCallCommon(mlir::Operation *op,
572625 }
573626 printer << " (" << ops << " )" ;
574627
575- printer.printOptionalAttrDict (op->getAttrs (), {" callee" });
628+ if (sideEffect != cir::SideEffect::All) {
629+ printer << " side_effect(" ;
630+ printer << stringifySideEffect (sideEffect);
631+ printer << " )" ;
632+ }
633+
634+ printer.printOptionalAttrDict (op->getAttrs (), {" callee" , " side_effect" });
576635
577636 printer << " : " ;
578637 printer.printFunctionalType (op->getOperands ().getTypes (),
@@ -586,7 +645,8 @@ mlir::ParseResult cir::CallOp::parse(mlir::OpAsmParser &parser,
586645
587646void cir::CallOp::print (mlir::OpAsmPrinter &p) {
588647 mlir::Value indirectCallee = isIndirect () ? getIndirectCall () : nullptr ;
589- printCallCommon (*this , getCalleeAttr (), indirectCallee, p);
648+ cir::SideEffect sideEffect = getSideEffect ();
649+ printCallCommon (*this , getCalleeAttr (), indirectCallee, p, sideEffect);
590650}
591651
592652static LogicalResult
0 commit comments