Skip to content

[clang][bytecode] Implement integral-to-fixed-point casts #110350

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

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,17 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
ToSize, CE);
};

case CK_IntegralToFixedPoint: {
if (!this->visit(SubExpr))
return false;

auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
uint32_t I;
std::memcpy(&I, &Sem, sizeof(Sem));
return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), I,
CE);
}

case CK_ToVoid:
return discard(SubExpr);

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ByteCode/FixedPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ using APSInt = llvm::APSInt;
class FixedPoint final {
private:
llvm::APFixedPoint V;
FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}

public:
FixedPoint(llvm::APFixedPoint &&V) : V(std::move(V)) {}
FixedPoint(llvm::APFixedPoint &V) : V(V) {}
FixedPoint(APInt V, llvm::FixedPointSemantics Sem) : V(V, Sem) {}
// This needs to be default-constructible so llvm::endian::read works.
FixedPoint()
Expand Down
29 changes: 29 additions & 0 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace clang {
namespace interp {

using APSInt = llvm::APSInt;
using FixedPointSemantics = llvm::FixedPointSemantics;

/// Convert a value to an APValue.
template <typename T>
Expand Down Expand Up @@ -2311,6 +2312,34 @@ static inline bool CastPointerIntegralAPS(InterpState &S, CodePtr OpPC,
return true;
}

template <PrimType Name, class T = typename PrimConv<Name>::T>
static inline bool CastIntegralFixedPoint(InterpState &S, CodePtr OpPC,
uint32_t FPS) {
const T &Int = S.Stk.pop<T>();

FixedPointSemantics Sem(0, 0, false, false, false);
std::memcpy(&Sem, &FPS, sizeof(Sem));

bool Overflow;
llvm::APFixedPoint IntResult =
llvm::APFixedPoint::getFromIntValue(Int.toAPSInt(), Sem, &Overflow);

if (Overflow) {
const Expr *E = S.Current->getExpr(OpPC);
if (S.checkingForUndefinedBehavior()) {
S.getASTContext().getDiagnostics().Report(
E->getExprLoc(), diag::warn_fixedpoint_constant_overflow)
<< IntResult.toString() << E->getType();
}
S.CCEDiag(E, diag::note_constexpr_overflow) << IntResult << E->getType();
if (!S.noteUndefinedBehavior())
return false;
}

S.Stk.push<FixedPoint>(IntResult);
return true;
}

static inline bool PtrPtrCast(InterpState &S, CodePtr OpPC, bool SrcIsVoidPtr) {
const auto &Ptr = S.Stk.peek<Pointer>();

Expand Down
6 changes: 6 additions & 0 deletions clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ def CastPointerIntegralAP : Opcode {
def CastPointerIntegralAPS : Opcode {
let Args = [ArgUint32];
}
def CastIntegralFixedPoint : Opcode {
let Types = [FixedSizeIntegralTypes];
let Args = [ArgUint32];
let HasGroup = 1;
}

def PtrPtrCast : Opcode {
let Args = [ArgBool];

Expand Down
8 changes: 8 additions & 0 deletions clang/test/AST/ByteCode/fixed-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ static_assert(-12.0k == -(-(-12.0k)));
/// Zero-init.
constexpr _Accum A{};
static_assert(A == 0.0k);

namespace IntToFixedPointCast {
constexpr _Accum B = 13;
static_assert(B == 13.0k);

constexpr _Fract sf = -1;
static_assert(sf == -1.0k);
}
Loading