Skip to content

Commit c374f5b

Browse files
tbaederrPhilippRados
authored andcommitted
[clang][bytecode] Fix discarding __builtin_bit_cast calls (llvm#114926)
Optionally prepare storage for the result and do the bitcast anyway, to get the right diagnostic output.
1 parent e37ab05 commit c374f5b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6446,8 +6446,6 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
64466446
QualType ToType = E->getType();
64476447
std::optional<PrimType> ToT = classify(ToType);
64486448

6449-
assert(!DiscardResult && "Implement DiscardResult mode for bitcasts.");
6450-
64516449
if (ToType->isNullPtrType()) {
64526450
if (!this->discard(SubExpr))
64536451
return false;
@@ -6463,12 +6461,24 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
64636461
}
64646462
assert(!ToType->isReferenceType());
64656463

6464+
// Prepare storage for the result in case we discard.
6465+
if (DiscardResult && !Initializing && !ToT) {
6466+
std::optional<unsigned> LocalIndex = allocateLocal(E);
6467+
if (!LocalIndex)
6468+
return false;
6469+
if (!this->emitGetPtrLocal(*LocalIndex, E))
6470+
return false;
6471+
}
6472+
64666473
// Get a pointer to the value-to-cast on the stack.
64676474
if (!this->visit(SubExpr))
64686475
return false;
64696476

6470-
if (!ToT || ToT == PT_Ptr)
6471-
return this->emitBitCastPtr(E);
6477+
if (!ToT || ToT == PT_Ptr) {
6478+
if (!this->emitBitCastPtr(E))
6479+
return false;
6480+
return DiscardResult ? this->emitPopPtr(E) : true;
6481+
}
64726482
assert(ToT);
64736483

64746484
const llvm::fltSemantics *TargetSemantics = nullptr;

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ constexpr Init round_trip(const Init &init) {
3838
return bit_cast<Init>(bit_cast<Intermediate>(init));
3939
}
4040

41+
42+
namespace Discarding {
43+
struct S { int a; };
44+
constexpr int f = (__builtin_bit_cast(int, 2), 0);
45+
constexpr int f2 = (__builtin_bit_cast(S, 2), 0);
46+
}
47+
4148
namespace std {
4249
enum byte : unsigned char {};
4350
} // namespace std

0 commit comments

Comments
 (0)