[CIR] Support CXXRewrittenBinaryOperator in emitLValue

Open Beginner friendly
#223,743 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

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.

Description

ClangIR
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:

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

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

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

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

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:

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():

case Expr::CXXRewrittenBinaryOperatorClass:
  return EmitLValue(cast<CXXRewrittenBinaryOperator>(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.

Dominant language
LLVM
Stars
40.6k
Forks
18.7k
Avg merge
18h 16m
Merged PRs (30d)
417

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from llvm/llvm-project

All issues in llvm/llvm-project

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.