@@ -330,40 +330,41 @@ Address AtomicInfo::CreateTempAlloca() const {
330330 return TempAlloca;
331331}
332332
333- // If the value comes from a ConstOp + IntAttr, retrieve and skip a series
334- // of casts if necessary.
335- //
336- // FIXME(cir): figure out warning issue and move this to CIRBaseBuilder.h
337- static cir::IntAttr getConstOpIntAttr (mlir::Value v) {
338- mlir::Operation *op = v.getDefiningOp ();
339- cir::IntAttr constVal;
340- while (auto c = dyn_cast<cir::CastOp>(op))
341- op = c.getOperand ().getDefiningOp ();
342- if (auto c = dyn_cast<cir::ConstantOp>(op)) {
343- if (mlir::isa<cir::IntType>(c.getType ()))
344- constVal = mlir::cast<cir::IntAttr>(c.getValue ());
345- }
346- return constVal;
333+ static mlir::Value stripCasts (mlir::Value value) {
334+ while (auto castOp = value.getDefiningOp <cir::CastOp>())
335+ value = castOp.getOperand ();
336+ return value;
337+ }
338+
339+ static cir::ConstantOp extractConstant (mlir::Value v) {
340+ return stripCasts (v).getDefiningOp <cir::ConstantOp>();
341+ }
342+
343+ // If the value comes from a ConstOp + IntAttr, retrieve and skip a series of
344+ // casts if necessary.
345+ static cir::IntAttr extractIntAttr (mlir::Value v) {
346+ if (auto c = extractConstant (v))
347+ return c.getValueAttr <cir::IntAttr>();
348+ return {};
347349}
348350
349351// Inspect a value that is the strong/weak flag for a compare-exchange. If it
350352// is a constant of intergral or boolean type, set `val` to the constant's
351353// boolean value and return true. Otherwise leave `val` unchanged and return
352354// false.
353355static bool isCstWeak (mlir::Value weakVal, bool &val) {
354- mlir::Operation *op = weakVal.getDefiningOp ();
355- while (auto c = dyn_cast<cir::CastOp>(op)) {
356- op = c.getOperand ().getDefiningOp ();
357- }
358- if (auto c = dyn_cast<cir::ConstantOp>(op)) {
359- if (mlir::isa<cir::IntType>(c.getType ())) {
360- val = mlir::cast<cir::IntAttr>(c.getValue ()).getUInt () != 0 ;
356+ if (auto c = extractConstant (weakVal)) {
357+ if (auto attr = c.getValueAttr <cir::IntAttr>()) {
358+ val = attr.getUInt () != 0 ;
361359 return true ;
362- } else if (mlir::isa<cir::BoolType>(c.getType ())) {
363- val = mlir::cast<cir::BoolAttr>(c.getValue ()).getValue ();
360+ }
361+
362+ if (auto attr = c.getValueAttr <cir::BoolAttr>()) {
363+ val = attr.getValue ();
364364 return true ;
365365 }
366366 }
367+
367368 return false ;
368369}
369370
@@ -456,7 +457,7 @@ static void emitAtomicCmpXchgFailureSet(
456457 cir::MemOrder SuccessOrder, cir::MemScopeKind Scope) {
457458
458459 cir::MemOrder FailureOrder;
459- if (auto ordAttr = getConstOpIntAttr (FailureOrderVal)) {
460+ if (auto ordAttr = extractIntAttr (FailureOrderVal)) {
460461 // We should not ever get to a case where the ordering isn't a valid CABI
461462 // value, but it's hard to enforce that in general.
462463 auto ord = ordAttr.getUInt ();
@@ -805,7 +806,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *Expr, Address Dest,
805806 }
806807
807808 // Handle constant scope.
808- if (getConstOpIntAttr (Scope)) {
809+ if (extractIntAttr (Scope)) {
809810 assert (!cir::MissingFeatures::syncScopeID ());
810811 llvm_unreachable (" NYI" );
811812 return ;
@@ -1208,7 +1209,7 @@ RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *E) {
12081209 E->getOp () == AtomicExpr::AO__scoped_atomic_load ||
12091210 E->getOp () == AtomicExpr::AO__scoped_atomic_load_n;
12101211
1211- if (auto ordAttr = getConstOpIntAttr (Order)) {
1212+ if (auto ordAttr = extractIntAttr (Order)) {
12121213 // We should not ever get to a case where the ordering isn't a valid CABI
12131214 // value, but it's hard to enforce that in general.
12141215 auto ord = ordAttr.getUInt ();
0 commit comments