[clang] [CIR] Upstream binary assignments and comma (PR #135115)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 10 13:11:05 PDT 2025
================
@@ -807,6 +808,65 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
VISITCOMP(EQ)
VISITCOMP(NE)
#undef VISITCOMP
+
+ mlir::Value VisitBinAssign(const BinaryOperator *e) {
+ const bool ignore = std::exchange(ignoreResultAssign, false);
+
+ mlir::Value rhs;
+ LValue lhs;
+
+ switch (e->getLHS()->getType().getObjCLifetime()) {
+ case Qualifiers::OCL_Strong:
+ case Qualifiers::OCL_Autoreleasing:
+ case Qualifiers::OCL_ExplicitNone:
+ case Qualifiers::OCL_Weak:
+ assert(!cir::MissingFeatures::objCLifetime());
+ break;
+ case Qualifiers::OCL_None:
+ // __block variables need to have the rhs evaluated first, plus this
+ // should improve codegen just a little.
+ rhs = Visit(e->getRHS());
+ assert(!cir::MissingFeatures::sanitizers());
+ // TODO(cir): This needs to be emitCheckedLValue() once we support
+ // sanitizers
+ lhs = cgf.emitLValue(e->getLHS());
+
+ // Store the value into the LHS. Bit-fields are handled specially because
+ // the result is altered by the store, i.e., [C99 6.5.16p1]
+ // 'An assignment expression has the value of the left operand after the
+ // assignment...'.
+ if (lhs.isBitField()) {
+ cgf.emitStoreThroughBitfieldLValue(RValue::get(rhs), lhs, rhs);
+ } else {
+ cgf.emitNullabilityCheck(lhs, rhs, e->getExprLoc());
+ CIRGenFunction::SourceLocRAIIObject loc{
+ cgf, cgf.getLoc(e->getSourceRange())};
+ cgf.emitStoreThroughLValue(RValue::get(rhs), lhs);
+ }
+ }
+
+ // If the result is clearly ignored, return now.
+ if (ignore)
+ return nullptr;
+
+ // The result of an assignment in C is the assigned r-value.
+ if (!cgf.getLangOpts().CPlusPlus)
+ return rhs;
+
+ // If the lvalue is non-volatile, return the computed value of the
+ // assignment.
+ if (!lhs.isVolatile())
+ return rhs;
+
+ // Otherwise, reload the value.
+ return emitLoadOfLValue(lhs, e->getExprLoc());
+ }
+
+ mlir::Value VisitBinComma(const BinaryOperator *e) {
+ cgf.emitIgnoredExpr(e->getLHS());
+ // NOTE: We don't need to EnsureInsertPoint() like LLVM codegen.
----------------
andykaylor wrote:
I think this is a difference in the way LLVM IR and MLIR are generated. You can create LLVM IR instructions in the void and insert them later. MLIR operations are always created in some region or block. The classic codegen calls `EnsureInsertPoint` to create a basic block to receive emitted instructions. It calls it all over the place. I'm not sure why we'd have a comment about it here. This is the only reference to `EnsureInsertPoint` in the incubator code.
https://github.com/llvm/llvm-project/pull/135115
More information about the cfe-commits
mailing list