Skip to content
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

auto-sync BitCastStdArray.py: Transform to union instead #2257

Merged
merged 2 commits into from
Jan 24, 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
33 changes: 24 additions & 9 deletions arch/AArch64/AArch64AddressingModes.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,12 @@ static inline uint64_t AArch64_AM_decodeAdvSIMDModImmType12(uint8_t Imm)
#define DEFINE_isSVEMaskOfIdenticalElements(T) \
static inline bool CONCAT(AArch64_AM_isSVEMaskOfIdenticalElements, T)(int64_t Imm) \
{ \
union { \
uint64_t L; \
T A[sizeof(int64_t) / sizeof(T)]; \
} U; \
U.L = Imm; \
T *Parts = U.A; \
union { \
int64_t In; \
T Out[sizeof(int64_t) / sizeof(T)]; \
} U_Parts; \
U_Parts.In = Imm; \
T *Parts = U_Parts.Out; \
for (int i = 0; i < (sizeof(int64_t) / sizeof(T)); i++) { \
if (Parts[i] != Parts[0]) \
return false; \
Expand Down Expand Up @@ -887,9 +887,24 @@ AArch64_AM_isSVEMoveMaskPreferredLogicalImmediate(int64_t Imm)
if (isSVECpyImm64(Imm))
return false;

int32_t *S = (int32_t *)(&(Imm)); // arr len = 2
int16_t *H = (int16_t *)(&(Imm)); // arr len = 4
int8_t *B = (int8_t *)(&(Imm)); // arr len = 8
union {
int64_t In;
int32_t Out[2];
} U_S;
U_S.In = Imm;
int32_t *S = U_S.Out;
union {
int64_t In;
int16_t Out[4];
} U_H;
U_H.In = Imm;
int16_t *H = U_H.Out;
union {
int64_t In;
int8_t Out[8];
} U_B;
U_B.In = Imm;
int8_t *B = U_B.Out;

if (CONCAT(AArch64_AM_isSVEMaskOfIdenticalElements, int32_t)(Imm) &&
isSVECpyImm32(S[0]))
Expand Down
20 changes: 17 additions & 3 deletions suite/auto-sync/Updater/CppTranslator/Patches/BitCastStdArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
class BitCastStdArray(Patch):
"""
Patch auto S = bit_cast<std::array<int32_t, 2>>(Imm);
to int32_t *S = ((int32_t *)(&Imm)); // Array length = 2
to union {
typeof(Imm) In;
int32_t Out[2];
} U_S;
U_S.In = Imm;
int32_t *S = U_S.Out;

MSVC doesn't support typeof so it has to be resolved manually.
"""

def __init__(self, priority: int):
Expand Down Expand Up @@ -38,8 +45,15 @@ def get_main_capture_name(self) -> str:
def get_patch(self, captures: [(Node, str)], src: bytes, **kwargs) -> bytes:
arr_name: bytes = captures[1][0].text
array_type: Node = captures[3][0]
cast_target: bytes = captures[4][0].text
cast_target: bytes = captures[4][0].text.strip(b"()")
array_templ_args: bytes = array_type.named_children[0].named_children[1].named_children[1].text.strip(b"<>")
arr_type = array_templ_args.split(b",")[0]
arr_len = array_templ_args.split(b",")[1]
return arr_type + b" *" + arr_name + b" = (" + arr_type + b"*)(&" + cast_target + b"); // arr len = " + arr_len
return (
b"union {\n"
+ b" typeof(" + cast_target + b") In;\n"
+ b" " + arr_type + b" Out[" + arr_len + b"];\n"
+ b"} U_" + arr_name + b";\n"
+ b"U_" + arr_name + b".In = " + cast_target + b";\n"
+ arr_type + b" *" + arr_name + b" = U_" + arr_name + b".Out;"
)