Skip to content

Commit 0611fdd

Browse files
authored
[clang][bytecode] Add simple __builtin_memcpy implementation (#118278)
Not handling all the special- and error cases yet. Just making sure the bitcast code can be used for this as well.
1 parent 4930f69 commit 0611fdd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,32 @@ static bool interp__builtin_vector_reduce(InterpState &S, CodePtr OpPC,
17231723
llvm_unreachable("Unsupported vector reduce builtin");
17241724
}
17251725

1726+
static bool interp__builtin_memcpy(InterpState &S, CodePtr OpPC,
1727+
const InterpFrame *Frame,
1728+
const Function *Func, const CallExpr *Call) {
1729+
assert(Call->getNumArgs() == 3);
1730+
Pointer DestPtr = getParam<Pointer>(Frame, 0);
1731+
const Pointer &SrcPtr = getParam<Pointer>(Frame, 1);
1732+
const APSInt &Size =
1733+
peekToAPSInt(S.Stk, *S.getContext().classify(Call->getArg(2)));
1734+
assert(!Size.isSigned() && "memcpy and friends take an unsigned size");
1735+
1736+
if (DestPtr.isDummy() || SrcPtr.isDummy())
1737+
return false;
1738+
1739+
// If the size is zero, we treat this as always being a valid no-op.
1740+
if (Size.isZero()) {
1741+
S.Stk.push<Pointer>(DestPtr);
1742+
return true;
1743+
}
1744+
1745+
if (!DoBitCastPtr(S, OpPC, SrcPtr, DestPtr))
1746+
return false;
1747+
1748+
S.Stk.push<Pointer>(DestPtr);
1749+
return true;
1750+
}
1751+
17261752
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
17271753
const CallExpr *Call, uint32_t BuiltinID) {
17281754
const InterpFrame *Frame = S.Current;
@@ -2173,6 +2199,11 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
21732199
return false;
21742200
break;
21752201

2202+
case Builtin::BI__builtin_memcpy:
2203+
if (!interp__builtin_memcpy(S, OpPC, Frame, F, Call))
2204+
return false;
2205+
break;
2206+
21762207
default:
21772208
S.FFDiag(S.Current->getLocation(OpPC),
21782209
diag::note_invalid_subexpr_in_const_expr)

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ namespace BuiltinInImplicitCtor {
991991
static_assert(Foo.a == 0, "");
992992
}
993993

994-
995994
typedef double vector4double __attribute__((__vector_size__(32)));
996995
typedef float vector4float __attribute__((__vector_size__(16)));
997996
typedef long long vector4long __attribute__((__vector_size__(32)));
@@ -1035,3 +1034,13 @@ namespace RecuceAdd {
10351034
static_assert(reduceAddInt3 == 0);
10361035
#endif
10371036
}
1037+
1038+
namespace BuiltinMemcpy {
1039+
constexpr int simple() {
1040+
int a = 12;
1041+
int b = 0;
1042+
__builtin_memcpy(&b, &a, sizeof(a));
1043+
return b;
1044+
}
1045+
static_assert(simple() == 12);
1046+
}

0 commit comments

Comments
 (0)