-
Notifications
You must be signed in to change notification settings - Fork 183
[CIR] Implement cast expression classes as l-values #2011
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
Draft
lanza
wants to merge
2
commits into
gh/lanza/12/base
Choose a base branch
from
gh/lanza/12/head
base: gh/lanza/12/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−18
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lanza
added a commit
that referenced
this pull request
Nov 23, 2025
This commit fixes l-value emission for CXXConstCastExpr, CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr, and ObjCBridgedCastExpr by routing them through the existing emitCastLValue() function, matching the behavior of CodeGen. Previously, these cast expression classes would trigger an error and assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`). This was an artificial limitation - CodeGen routes all cast expression classes through EmitCastLValue(), which then handles each cast *kind* appropriately. Key insight from CodeGen (CGExpr.cpp:5679-5685): "Casts are never lvalues unless that cast is to a reference type." The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr) doesn't determine if it's a valid l-value. Instead, the cast *kind* (CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity. emitCastLValue() already handles this correctly based on getCastKind(). Implementation Details: - Removed emitError() and assert() for const_cast and related expr classes - Unified all cast expression classes in emitLValue() switch statement - All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832) - Safety is maintained by emitCastLValue()'s cast kind handling This follows the ClangIR principle of copying implementations from CodeGen, which has been proven correct over 20 years. Testing: - Single comprehensive test covering const_cast and reinterpret_cast - Tests verify CIR emission, LLVM lowering, and match with original CodeGen - All three outputs verified to ensure correctness Test Plan: $ ninja -C build/Release clang $ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp ghstack-source-id: f4cfd63 Pull-Request: #2011
lanza
added a commit
that referenced
this pull request
Nov 24, 2025
This commit fixes l-value emission for CXXConstCastExpr, CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr, and ObjCBridgedCastExpr by routing them through the existing emitCastLValue() function, matching the behavior of CodeGen. Previously, these cast expression classes would trigger an error and assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`). This was an artificial limitation - CodeGen routes all cast expression classes through EmitCastLValue(), which then handles each cast *kind* appropriately. Key insight from CodeGen (CGExpr.cpp:5679-5685): "Casts are never lvalues unless that cast is to a reference type." The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr) doesn't determine if it's a valid l-value. Instead, the cast *kind* (CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity. emitCastLValue() already handles this correctly based on getCastKind(). Implementation Details: - Removed emitError() and assert() for const_cast and related expr classes - Unified all cast expression classes in emitLValue() switch statement - All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832) - Safety is maintained by emitCastLValue()'s cast kind handling This follows the ClangIR principle of copying implementations from CodeGen, which has been proven correct over 20 years. Testing: - Single comprehensive test covering const_cast and reinterpret_cast - Tests verify CIR emission, LLVM lowering, and match with original CodeGen - All three outputs verified to ensure correctness Test Plan: $ ninja -C build/Release clang $ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp ghstack-source-id: f4cfd63 Pull-Request: #2011
This was referenced Nov 24, 2025
Draft
lanza
added a commit
that referenced
this pull request
Nov 25, 2025
This commit fixes l-value emission for CXXConstCastExpr, CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr, and ObjCBridgedCastExpr by routing them through the existing emitCastLValue() function, matching the behavior of CodeGen. Previously, these cast expression classes would trigger an error and assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`). This was an artificial limitation - CodeGen routes all cast expression classes through EmitCastLValue(), which then handles each cast *kind* appropriately. Key insight from CodeGen (CGExpr.cpp:5679-5685): "Casts are never lvalues unless that cast is to a reference type." The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr) doesn't determine if it's a valid l-value. Instead, the cast *kind* (CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity. emitCastLValue() already handles this correctly based on getCastKind(). Implementation Details: - Removed emitError() and assert() for const_cast and related expr classes - Unified all cast expression classes in emitLValue() switch statement - All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832) - Safety is maintained by emitCastLValue()'s cast kind handling This follows the ClangIR principle of copying implementations from CodeGen, which has been proven correct over 20 years. Testing: - Single comprehensive test covering const_cast and reinterpret_cast - Tests verify CIR emission, LLVM lowering, and match with original CodeGen - All three outputs verified to ensure correctness Test Plan: $ ninja -C build/Release clang $ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp ghstack-source-id: f4cfd63 Pull-Request: #2011
lanza
added a commit
that referenced
this pull request
Nov 25, 2025
This commit fixes l-value emission for CXXConstCastExpr, CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr, and ObjCBridgedCastExpr by routing them through the existing emitCastLValue() function, matching the behavior of CodeGen. Previously, these cast expression classes would trigger an error and assertion when used in l-value contexts (e.g., `const_cast<int&>(x) = 1`). This was an artificial limitation - CodeGen routes all cast expression classes through EmitCastLValue(), which then handles each cast *kind* appropriately. Key insight from CodeGen (CGExpr.cpp:5679-5685): "Casts are never lvalues unless that cast is to a reference type." The cast expression *class* (CXXConstCastExpr vs CXXReinterpretCastExpr) doesn't determine if it's a valid l-value. Instead, the cast *kind* (CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity. emitCastLValue() already handles this correctly based on getCastKind(). Implementation Details: - Removed emitError() and assert() for const_cast and related expr classes - Unified all cast expression classes in emitLValue() switch statement - All now route through emitCastLValue() like CodeGen (CGExpr.cpp:1823-1832) - Safety is maintained by emitCastLValue()'s cast kind handling This follows the ClangIR principle of copying implementations from CodeGen, which has been proven correct over 20 years. Testing: - Single comprehensive test covering const_cast and reinterpret_cast - Tests verify CIR emission, LLVM lowering, and match with original CodeGen - All three outputs verified to ensure correctness Test Plan: $ ninja -C build/Release clang $ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp ghstack-source-id: f4cfd63 Pull-Request: #2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Stack from ghstack (oldest at bottom):
This commit fixes l-value emission for CXXConstCastExpr,
CXXReinterpretCastExpr, CXXFunctionalCastExpr, CXXAddrspaceCastExpr,
and ObjCBridgedCastExpr by routing them through the existing
emitCastLValue() function, matching the behavior of CodeGen.
Previously, these cast expression classes would trigger an error and
assertion when used in l-value contexts (e.g.,
const_cast<int&>(x) = 1).This was an artificial limitation - CodeGen routes all cast expression
classes through EmitCastLValue(), which then handles each cast kind
appropriately.
Key insight from CodeGen (CGExpr.cpp:5679-5685):
"Casts are never lvalues unless that cast is to a reference type."
The cast expression class (CXXConstCastExpr vs CXXReinterpretCastExpr)
doesn't determine if it's a valid l-value. Instead, the cast kind
(CK_NoOp, CK_LValueBitCast, etc.) and result type determine validity.
emitCastLValue() already handles this correctly based on getCastKind().
Implementation Details:
This follows the ClangIR principle of copying implementations from
CodeGen, which has been proven correct over 20 years.
Testing:
Test Plan:
$ ninja -C build/Release clang
$ build/Release/bin/llvm-lit clang/test/CIR/CodeGen/cast-lvalue.cpp