Skip to content

Conversation

rapidsna
Copy link

Solution from #10969:

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

Issue with the solution:

The issue #10969 got reverted due "error: cannot compile this l-value expression yet" occurred when __terminated_by_to_indexable is wrapped in an OVE.

Fix for the new issue with __terminated_by_to_indexable:

The fix is to implement "TerminatedByToIndexableExprLValue" so the codegen can also emit as if it's an lvalue.

rdar://146329029

@rapidsna
Copy link
Author

@swift-ci test llvm

@delcypher delcypher added the clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang label Sep 22, 2025
@rapidsna
Copy link
Author

CI seems to be blocked on rdar://161075623 ([BlueDragon] Build failed, AssertionError: {'CXCursor_LastExpr', 'CXCursor_ForgePtrExpr'} variants are missing.)

@rapidsna
Copy link
Author

@swift-ci test llvm

ElemBitCast);
: nullptr;

EmitWidePointerToDest(E->getType(), Ptr, Upper, Lower, ElemBitCast);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any behavioral change here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hnrklssn No behavioral change here. This is just to avoid execution order of arguments, which is an unspecified behavior, triggering non-determinism for codegen, and CI failures on other platforms.


LValue CodeGenFunction::EmitTerminatedByToIndexableExprLValue(const TerminatedByToIndexableExpr *E) {
if (!E->getType()->isPointerTypeWithBounds())
return EmitUnsupportedLValue(E, "l-value expression");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Sema guard against this occurring, or is this something that can actually happen in real code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hnrklssn Thanks for calling this out. This is not supposed to happen here.

I'll remove it. The assertion from EmitAggExprToLValue should be sufficient.

@rapidsna
Copy link
Author

These are failing in next (b9b0b72) and unrelated to this change.

10:58:32 ********************
10:58:32 Failed Tests (6):
10:58:32 Clang :: BoundsSafety/AST/attributes_in_template_decls_attr_only_mode.cpp
10:58:32 Clang :: BoundsSafety/CodeGen/arithmetic-ops-in-counted-by-assign-O2.c
10:58:32 Clang :: ClangScanDeps/optimize-canonicalize-macros.m
10:58:32 Clang :: Misc/warning-flags.c
10:58:32 LLVM :: Bitcode/compatibility.ll
10:58:32 LLVM :: Transforms/PhaseOrdering/X86/excessive-unrolling.ll

@rapidsna rapidsna force-pushed the eng/PR-146329029_swift_next branch from 95c5ed9 to 250f73c Compare September 23, 2025 20:57
@rapidsna
Copy link
Author

@swift-ci test llvm

@rapidsna
Copy link
Author

@hnrklssn Do you have any other comments?

Copy link

@hnrklssn hnrklssn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, LGTM!

@rapidsna rapidsna merged commit 04da1af into next Sep 24, 2025
0 of 2 checks passed
@rapidsna rapidsna deleted the eng/PR-146329029_swift_next branch September 24, 2025 22:03
rapidsna added a commit that referenced this pull request Sep 26, 2025
… temporary locations (#11469)

- Solution from #10969:

This should fix a miscompilation bug found during adoption. When a base is lvalue and is wrapped in OVE, the compiler created a temporary and incorrectly mapped the temporary's address as the lvalue instead of the actual address of the lvalue.

The solution is to emit lvalue directly instead of creating extra temporary.

- Issue with the solution:

The issue #10969 got originally reverted due "error: cannot compile this l-value expression yet" occurred when __terminated_by_to_indexable is wrapped in an OVE.

- Fix for the new issue with __terminated_by_to_indexable:

The fix is to implement "TerminatedByToIndexableExprLValue" so the codegen can also emit as if it's an lvalue.

rdar://146329029
(cherry picked from commit 04da1af)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bounds-safety Issue relating to the experimental -fbounds-safety feature in Clang
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants