llvm / llvm/llvm-project

[CIR] Support CXXRewrittenBinaryOperator in emitLValue

Open Beginner friendly
#223,743 2 comments 0 reactions 0 assignees View on GitHub
ClangIR
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

### Description

When generating ClangIR for a C++20 rewritten binary operator (e.g. rewritten comparison using `operator<=>`) whose evaluation produces an lvalue (such as binding a reference to the result), ClangIR emits a "Not Yet Implemented" error:

```text
error: ClangIR code gen Not Yet Implemented: emitLValue: CXXRewrittenBinaryOperator
```

This is located in `clang/lib/CIR/CodeGen/CIRGenFunction.cpp` under `CIRGenFunction::emitLValue`:

```cpp
case Expr::CXXRewrittenBinaryOperatorClass:
getCIRGenModule().errorNYI(e->getSourceRange(),
"emitLValue: CXXRewrittenBinaryOperator");
return LValue();
```

### Minimal Reproducer

Compiling the following C++20 test case with `-fclangir -emit-cir`:

```cpp
struct SpaceshipLValueResult {
int &operator<(int) const;
};

struct LValueItem {
SpaceshipLValueResult operator<=>(const LValueItem &) const;
};

void cxx_rewritten_binary_operator_lvalue_expr() {
LValueItem a;
LValueItem b;
int &ref = (a < b);
}
```

Output:
```text
error: ClangIR code gen Not Yet Implemented: emitLValue: CXXRewrittenBinaryOperator
```

### Reference Implementation

In classic Clang CodeGen (`clang/lib/CodeGen/CGExpr.cpp`), `CXXRewrittenBinaryOperator` evaluates its underlying semantic form via `getSemanticForm()`:

```cpp
case Expr::CXXRewrittenBinaryOperatorClass:
return EmitLValue(cast(E)->getSemanticForm(),
IsKnownNonNull);
```

Other expression emitters in ClangIR already implement `CXXRewrittenBinaryOperator` by delegating to `getSemanticForm()`:
- `clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp` (`VisitCXXRewrittenBinaryOperator`)
- `clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp` (`VisitCXXRewrittenBinaryOperator`)
- `clang/lib/CIR/CodeGen/CIRGenExprComplex.cpp` (`VisitCXXRewrittenBinaryOperator`)

Supporting `CXXRewrittenBinaryOperator` in `CIRGenFunction::emitLValue` (`clang/lib/CIR/CodeGen/CIRGenFunction.cpp`) and adding corresponding test cases to `clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp` will bring parity with classic CodeGen and complete coverage for rewritten binary operators across all CIR expression emitters.

Contributor guide

Open the contributing guide

Research direction

Start in clang/lib/CIR/CodeGen/CIRGenFunction.cpp at CIRGenFunction::emitLValue and compare the classic Clang handling in clang/lib/CodeGen/CGExpr.cpp. Review the existing rewritten-operator visitors in CIRGenExprScalar.cpp, CIRGenExprAggregate.cpp, and CIRGenExprComplex.cpp, then add coverage in clang/test/CIR/CodeGen/cxx-rewritten-binary-operator.cpp. Done means the reproducer no longer emits the Not Yet Implemented error and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.