Skip to content

[CIR][NFCI] Represent Complex RValues As Single Value #144519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
mlir::Value v;
if (arg.isAggregate())
cgm.errorNYI(loc, "emitCall: aggregate call argument");
v = arg.getKnownRValue().getScalarVal();
v = arg.getKnownRValue().getValue();

// We might have to widen integers, but we should never truncate.
if (argType != v.getType() && mlir::isa<cir::IntType>(v.getType()))
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ void CIRGenFunction::emitStoreThroughLValue(RValue src, LValue dst,
const mlir::Value vector =
builder.createLoad(loc, dst.getVectorAddress());
const mlir::Value newVector = builder.create<cir::VecInsertOp>(
loc, vector, src.getScalarVal(), dst.getVectorIdx());
loc, vector, src.getValue(), dst.getVectorIdx());
builder.createStore(loc, newVector, dst.getVectorAddress());
return;
}
Expand All @@ -232,7 +232,7 @@ void CIRGenFunction::emitStoreThroughLValue(RValue src, LValue dst,
assert(!cir::MissingFeatures::opLoadStoreObjC());

assert(src.isScalar() && "Can't emit an aggregate store with this method");
emitStoreOfScalar(src.getScalarVal(), dst, isInit);
emitStoreOfScalar(src.getValue(), dst, isInit);
}

static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
Expand Down Expand Up @@ -949,7 +949,7 @@ LValue CIRGenFunction::emitCallExprLValue(const CallExpr *e) {
"Can't have a scalar return unless the return type is a "
"reference type!");

return makeNaturalAlignPointeeAddrLValue(rv.getScalarVal(), e->getType());
return makeNaturalAlignPointeeAddrLValue(rv.getValue(), e->getType());
}

LValue CIRGenFunction::emitBinaryOperatorLValue(const BinaryOperator *e) {
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
mlir::Value emitLoadOfLValue(const Expr *e) {
LValue lv = cgf.emitLValue(e);
// FIXME: add some akin to EmitLValueAlignmentAssumption(E, V);
return cgf.emitLoadOfLValue(lv, e->getExprLoc()).getScalarVal();
return cgf.emitLoadOfLValue(lv, e->getExprLoc()).getValue();
}

mlir::Value emitLoadOfLValue(LValue lv, SourceLocation loc) {
return cgf.emitLoadOfLValue(lv, loc).getScalarVal();
return cgf.emitLoadOfLValue(lv, loc).getValue();
}

// l-values
Expand Down Expand Up @@ -400,10 +400,10 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
cgf.cgm.errorNYI(e->getSourceRange(), "Atomic inc/dec");
// TODO(cir): This is not correct, but it will produce reasonable code
// until atomic operations are implemented.
value = cgf.emitLoadOfLValue(lv, e->getExprLoc()).getScalarVal();
value = cgf.emitLoadOfLValue(lv, e->getExprLoc()).getValue();
input = value;
} else {
value = cgf.emitLoadOfLValue(lv, e->getExprLoc()).getScalarVal();
value = cgf.emitLoadOfLValue(lv, e->getExprLoc()).getValue();
input = value;
}

Expand Down Expand Up @@ -1805,7 +1805,7 @@ mlir::Value ScalarExprEmitter::VisitCallExpr(const CallExpr *e) {
if (e->getCallReturnType(cgf.getContext())->isReferenceType())
return emitLoadOfLValue(e);

auto v = cgf.emitCallExpr(e).getScalarVal();
auto v = cgf.emitCallExpr(e).getValue();
assert(!cir::MissingFeatures::emitLValueAlignmentAssumption());
return v;
}
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ mlir::LogicalResult CIRGenFunction::emitReturnStmt(const ReturnStmt &s) {
// If this function returns a reference, take the address of the
// expression rather than the value.
RValue result = emitReferenceBindingToExpr(rv);
builder.CIRBaseBuilderTy::createStore(loc, result.getScalarVal(),
*fnRetAlloca);
builder.CIRBaseBuilderTy::createStore(loc, result.getValue(), *fnRetAlloca);
} else {
mlir::Value value = nullptr;
switch (CIRGenFunction::getEvaluationKind(rv->getType())) {
Expand Down
27 changes: 8 additions & 19 deletions clang/lib/CIR/CodeGen/CIRGenValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ class RValue {
enum Flavor { Scalar, Complex, Aggregate };

union {
// Stores first and second value.
struct {
mlir::Value first;
mlir::Value second;
} vals;
mlir::Value value;

// Stores aggregate address.
Address aggregateAddr;
Expand All @@ -47,7 +43,7 @@ class RValue {
unsigned flavor : 2;

public:
RValue() : vals{nullptr, nullptr}, flavor(Scalar) {}
RValue() : value(nullptr), flavor(Scalar) {}

bool isScalar() const { return flavor == Scalar; }
bool isComplex() const { return flavor == Complex; }
Expand All @@ -56,14 +52,9 @@ class RValue {
bool isVolatileQualified() const { return isVolatile; }

/// Return the value of this scalar value.
mlir::Value getScalarVal() const {
mlir::Value getValue() const {
assert(isScalar() && "Not a scalar!");
return vals.first;
}

/// Return the real/imag components of this complex value.
std::pair<mlir::Value, mlir::Value> getComplexVal() const {
return std::make_pair(vals.first, vals.second);
return value;
}

/// Return the value of the address of the aggregate.
Expand All @@ -83,22 +74,20 @@ class RValue {

static RValue get(mlir::Value v) {
RValue er;
er.vals.first = v;
er.value = v;
er.flavor = Scalar;
er.isVolatile = false;
return er;
}

static RValue getComplex(mlir::Value v1, mlir::Value v2) {
static RValue getComplex(mlir::Value v) {
RValue er;
er.vals = {v1, v2};
er.value = v;
er.flavor = Complex;
er.isVolatile = false;
return er;
}
static RValue getComplex(const std::pair<mlir::Value, mlir::Value> &c) {
return getComplex(c.first, c.second);
}

// FIXME: Aggregate rvalues need to retain information about whether they are
// volatile or not. Remove default to find all places that probably get this
// wrong.
Expand Down
Loading