Skip to content
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
3 changes: 2 additions & 1 deletion .github/scripts/download-bundled/jit-ir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cd "$(dirname "$0")/../../.."
tmp_dir=/tmp/php-src-download-bundled/jit-ir
rm -rf "$tmp_dir"

revision=281a66a87aedc4444ca09f696b9b8e9dab89ad32
revision=b8a6cee5bbffea2299756a932b2388345dacd5ba

git clone --depth 1 --revision="$revision" https://github.com/dstogov/ir.git "$tmp_dir"

Expand All @@ -22,6 +22,7 @@ rm -r examples
rm -r tests
rm -r tools
rm -r fuzz
rm -r llvm_tests
rm README.md
rm TODO
rm ir.g
Expand Down
9 changes: 9 additions & 0 deletions ext/opcache/jit/ir/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.o
libir.a
*.log
*.dot
*.pdf
Expand All @@ -18,8 +19,16 @@ tests/**/*.ir
tests/**/*.out
tests/**/*.log

llvm_tests/**/*.diff
llvm_tests/**/*.exp
llvm_tests/**/*.ll
llvm_tests/**/*.out

win32/vcpkg
win32/build_*

fuzz/build/
fuzz/corpus/

# macOS debug symbols (clang -g)
*.dSYM/
6 changes: 3 additions & 3 deletions ext/opcache/jit/ir/dynasm/dasm_arm64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ map_op = {
["strh_*"] = "78000000DwL",
["ldrh_*"] = "78400000DwL",
["ldrsh_*"] = "78c00000DwL|78800000DxL",
["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL|3c800000DqL",
["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|9c000000DqB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL|3cc00000DqL",
["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL|3c800000DqL|3c000000DbL|7c000000DhL",
["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|9c000000DqB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL|3cc00000DqL|3c400000DbL|7c400000DhL",
["ldrsw_*"] = "98000000DxB|b8800000DxL",
-- NOTE: ldur etc. are handled by ldr et al.

Expand Down Expand Up @@ -962,7 +962,7 @@ local function parse_template(params, template, nparams, pos)
werror("bad register type")
end
parse_reg_type = false
elseif p == "x" or p == "w" or p == "d" or p == "s" or p == "q" then
elseif p == "x" or p == "w" or p == "d" or p == "s" or p == "q" or p == "h" or p == "b" then
if parse_reg_type ~= p then
werror("register size mismatch")
end
Expand Down
94 changes: 42 additions & 52 deletions ext/opcache/jit/ir/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,61 +114,49 @@ void ir_print_escaped_str(const char *s, size_t len, FILE *f)
}
}

static void ir_print_double(double v, FILE *f)
static void ir_print_double(const double *v, FILE *f)
{
char buf[128];

if (isnan(v)) {
union {
double d;
long long i;
} u1, u2, u3;
u1.d = v;
u2.d = NAN;
u3.d = -NAN;
if (u1.i == u2.i) {
fprintf(f, "nan");
} else if (u1.i == u3.i) {
if (isnan(*v)) {
if (*(uint64_t*)(v) == 0xfff8000000000000) {
fprintf(f, "-nan");
} else {
fprintf(f, "nan(0x%llx)", u1.i);
fprintf(f, "nan");
if (*(uint64_t*)(v) & 0x8007ffffffffffff) {
fprintf(f, "(0x%" PRIx64 ")", *(uint64_t*)(v));
}
}
} else {
snprintf(buf, sizeof(buf), "%g", v);
if (strtod(buf, NULL) != v) {
snprintf(buf, sizeof(buf), "%.53e", v);
if (strtod(buf, NULL) != v) {
snprintf(buf, sizeof(buf), "%g", *v);
if (strtod(buf, NULL) != *v) {
snprintf(buf, sizeof(buf), "%.53e", *v);
if (strtod(buf, NULL) != *v) {
IR_ASSERT(0 && "can't format double");
}
}
fprintf(f, "%s", buf);
}
}

static void ir_print_float(float v, FILE *f)
static void ir_print_float(const float *v, FILE *f)
{
char buf[128];

if (isnan(v)) {
union {
float f;
int i;
} u1, u2, u3;
u1.f = v;
u2.f = NAN;
u3.f = -NAN;
if (u1.i == u2.i) {
fprintf(f, "nan");
} else if (u1.i == u3.i) {
if (isnan(*v)) {
if (*(uint32_t*)(v) == 0xffc00000) {
fprintf(f, "-nan");
} else {
fprintf(f, "nan(0x%x)", u1.i);
fprintf(f, "nan");
if (*(uint32_t*)(v) & 0x803fffff) {
fprintf(f, "(0x%x)", *(uint32_t*)(v));
}
}
} else {
snprintf(buf, sizeof(buf), "%g", v);
if (strtod(buf, NULL) != v) {
snprintf(buf, sizeof(buf), "%.24e", v);
if (strtod(buf, NULL) != v) {
snprintf(buf, sizeof(buf), "%g", *v);
if (strtod(buf, NULL) != *v) {
snprintf(buf, sizeof(buf), "%.24e", *v);
if (strtod(buf, NULL) != *v) {
IR_ASSERT(0 && "can't format float");
}
}
Expand Down Expand Up @@ -260,19 +248,19 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
}
break;
case IR_DOUBLE:
ir_print_double(*(double*)p, f);
ir_print_double((double*)p, f);
while (--n) {
p = (char*)p + sizeof(double);
fprintf(f, ", ");
ir_print_double(*(double*)p, f);
ir_print_double((double*)p, f);
}
break;
case IR_FLOAT:
ir_print_float(*(float*)p, f);
ir_print_float((float*)p, f);
while (--n) {
p = (char*)p + sizeof(float);
fprintf(f, ", ");
ir_print_float(*(float*)p, f);
ir_print_float((float*)p, f);
}
break;
default:
Expand Down Expand Up @@ -339,10 +327,10 @@ void ir_print_const(const ir_ctx *ctx, const ir_insn *insn, FILE *f, bool quoted
fprintf(f, "%" PRIi64, insn->val.i64);
break;
case IR_DOUBLE:
ir_print_double(insn->val.d, f);
ir_print_double(&insn->val.d, f);
break;
case IR_FLOAT:
ir_print_float(insn->val.f, f);
ir_print_float(&insn->val.f, f);
break;
default:
IR_ASSERT(0);
Expand Down Expand Up @@ -2416,7 +2404,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_load_i(const ir_ctx *ctx, ir_ref ref, i

while (ref > limit) {
insn = &ctx->ir_base[ref];
if (insn->op == IR_LOAD) {
if (insn->op == IR_LOAD || insn->op == IR_LOAD_v) {
if (insn->op2 == addr) {
if (insn->type == type) {
return ref; /* load forwarding (L2L) */
Expand All @@ -2427,7 +2415,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_load_i(const ir_ctx *ctx, ir_ref ref, i
return ref; /* partial load forwarding (L2L) */
}
}
} else if (insn->op == IR_STORE) {
} else if (insn->op == IR_STORE || insn->op == IR_STORE_v) {
ir_type type2 = ctx->ir_base[insn->op3].type;

if (insn->op2 == addr) {
Expand Down Expand Up @@ -2492,7 +2480,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vload_i(const ir_ctx *ctx, ir_ref ref,

while (ref > var) {
insn = &ctx->ir_base[ref];
if (insn->op == IR_VLOAD) {
if (insn->op == IR_VLOAD || insn->op == IR_VLOAD_v) {
if (insn->op2 == var) {
if (insn->type == type) {
return ref; /* load forwarding (L2L) */
Expand All @@ -2503,7 +2491,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vload_i(const ir_ctx *ctx, ir_ref ref,
return ref; /* partial load forwarding (L2L) */
}
}
} else if (insn->op == IR_VSTORE) {
} else if (insn->op == IR_VSTORE || insn->op == IR_VSTORE_v) {
ir_type type2 = ctx->ir_base[insn->op3].type;

if (insn->op2 == var) {
Expand Down Expand Up @@ -2574,14 +2562,14 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref

while (ref > limit) {
insn = &ctx->ir_base[ref];
if (insn->op == IR_STORE) {
if (insn->op == IR_STORE || insn->op == IR_STORE_v) {
if (insn->op2 == addr) {
if (ctx->ir_base[insn->op3].type == type) {
if (insn->op3 == val) {
/* dead STORE (store the same value once again) */
return ref;
} else {
if (!guarded) {
if (!guarded && insn->op == IR_STORE) {
/* the previous STORE is dead (there are no LOADs) */
if (!ctx->use_lists) {
if (next) {
Expand All @@ -2607,7 +2595,8 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref
}
if (!IR_IS_CONST_REF(insn->op3)) {
ir_use_list_remove_one(ctx, insn->op3, ref);
if (ctx->iter_worklist && ctx->use_lists[insn->op3].count == 0) {
if (ctx->iter_worklist && (ctx->use_lists[insn->op3].count == 0
|| ir_is_dead_load(ctx, insn->op3))) {
ir_bitqueue_add(ctx->iter_worklist, insn->op3);
}
}
Expand All @@ -2624,7 +2613,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_store_i(ir_ctx *ctx, ir_ref ref, ir_ref
type2 = ctx->ir_base[insn->op3].type;
goto check_aliasing;
}
} else if (insn->op == IR_LOAD) {
} else if (insn->op == IR_LOAD || insn->op == IR_LOAD_v) {
if (insn->op2 == addr) {
if (ref == val) {
/* dead STORE (store the value that was loaded before) */
Expand Down Expand Up @@ -2681,13 +2670,13 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re

while (ref > limit) {
insn = &ctx->ir_base[ref];
if (insn->op == IR_VSTORE) {
if (insn->op == IR_VSTORE || insn->op == IR_VSTORE_v) {
if (insn->op2 == var) {
if (insn->op3 == val) {
/* dead VSTORE */
return ref;
} else {
if (!guarded) {
if (!guarded && insn->op == IR_VSTORE) {
/* the previous VSTORE is dead (there are no VLOADs) */
if (!ctx->use_lists) {
if (next) {
Expand All @@ -2710,7 +2699,8 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re
}
if (!IR_IS_CONST_REF(insn->op3)) {
ir_use_list_remove_one(ctx, insn->op3, ref);
if (ctx->iter_worklist && ctx->use_lists[insn->op3].count == 0) {
if (ctx->iter_worklist && (ctx->use_lists[insn->op3].count == 0
|| ir_is_dead_load(ctx, insn->op3))) {
ir_bitqueue_add(ctx->iter_worklist, insn->op3);
}
}
Expand All @@ -2721,7 +2711,7 @@ IR_ALWAYS_INLINE ir_ref ir_find_aliasing_vstore_i(ir_ctx *ctx, ir_ref ref, ir_re
break;
}
}
} else if (insn->op == IR_VLOAD) {
} else if (insn->op == IR_VLOAD || insn->op == IR_VLOAD_v) {
if (insn->op2 == var) {
if (ref == val) {
/* dead VSTORE */
Expand Down
36 changes: 24 additions & 12 deletions ext/opcache/jit/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -665,16 +665,7 @@ void ir_strtab_free(ir_strtab *strtab);
#define IR_OPT_MEM2SSA (1<<22)
#define IR_OPT_CODEGEN (1<<23)
#define IR_OPT_TAILCALL (1<<24)

/* debug related */
#ifdef IR_DEBUG
# define IR_DEBUG_SCCP (1<<26)
# define IR_DEBUG_GCM (1<<27)
# define IR_DEBUG_GCM_SPLIT (1<<28)
# define IR_DEBUG_SCHEDULE (1<<29)
# define IR_DEBUG_RA (1<<30)
# define IR_DEBUG_BB_SCHEDULE (1U<<31)
#endif
#define IR_NO_OPT (1<<25)

/* Calling Conventions */
#define IR_CC_DEFAULT 0x00
Expand All @@ -694,6 +685,15 @@ void ir_strtab_free(ir_strtab *strtab);
#define IR_BUILTIN_FUNC IR_CC_BUILTIN
#define IR_FASTCALL_FUNC IR_CC_FASTCALL

/* Flags for ir_ctx.debug_flags (they work only in "debug" build) */
#define IR_DEBUG_SCCP (1<<0)
#define IR_DEBUG_GCM (1<<1)
#define IR_DEBUG_GCM_SPLIT (1<<2)
#define IR_DEBUG_SCHEDULE (1<<3)
#define IR_DEBUG_RA (1<<4)
#define IR_DEBUG_BB_SCHEDULE (1<<5)
#define IR_DEBUG_DESSA (1<<6)

typedef struct _ir_ctx ir_ctx;
typedef struct _ir_use_list ir_use_list;
typedef struct _ir_block ir_block;
Expand Down Expand Up @@ -738,6 +738,7 @@ struct _ir_ctx {
uint32_t flags2; /* IR context private flags (see IR_* defines in ir_private.h) */
ir_type ret_type; /* Function return type */
uint32_t mflags; /* CPU specific flags (see IR_X86_... macros below) */
uint32_t debug_flags; /* Debug flags (see IR_DEBUG_... macros, work only in DEBUG build) */
int32_t status; /* non-zero error code (see IR_ERROR_... macros), app may use negative codes */
ir_ref fold_cse_limit; /* CSE finds identical insns backward from "insn_count" to "fold_cse_limit" */
ir_insn fold_insn; /* temporary storage for folding engine */
Expand Down Expand Up @@ -1087,12 +1088,22 @@ void ir_dump_codegen(const ir_ctx *ctx, FILE *f);
/* IR to C conversion (implementation in ir_emit_c.c) */
int ir_emit_c(ir_ctx *ctx, const char *name, FILE *f);
void ir_emit_c_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f);
void ir_emit_c_sym_decl(const char *name, uint32_t flags, FILE *f);
void ir_emit_c_sym_decl(const char *name, uint32_t flags, size_t size, FILE *f);
void ir_emit_c_sym_data_str(size_t pos, const char *str, size_t len, FILE *f);
void ir_emit_c_sym_data(size_t pos, const void *data, size_t size, FILE *f);
void ir_emit_c_sym_data_pad(size_t pos, size_t size, FILE *f);
void ir_emit_c_sym_data_ref(size_t pos, ir_op op, const char *ref, uintptr_t offset, FILE *f);
void ir_emit_c_sym_data_end(uint32_t flags, FILE *f);

/* IR to LLVM conversion (implementation in ir_emit_llvm.c) */
int ir_emit_llvm(ir_ctx *ctx, const char *name, FILE *f);
void ir_emit_llvm_func_decl(const char *name, uint32_t flags, ir_type ret_type, uint32_t params_count, const uint8_t *param_types, FILE *f);
void ir_emit_llvm_sym_decl(const char *name, uint32_t flags, FILE *f);
void ir_emit_llvm_sym_decl(const char *name, uint32_t flags, size_t size, FILE *f);
void ir_emit_llvm_sym_data_str(size_t pos, const char *str, size_t len, FILE *f);
void ir_emit_llvm_sym_data(size_t pos, const void *data, size_t size, FILE *f);
void ir_emit_llvm_sym_data_pad(size_t pos, size_t size, FILE *f);
void ir_emit_llvm_sym_data_ref(size_t pos, ir_op op, const char *ref, uintptr_t offset, FILE *f);
void ir_emit_llvm_sym_data_end(uint32_t flags, FILE *f);

/* IR verification API (implementation in ir_check.c) */
bool ir_check(const ir_ctx *ctx);
Expand Down Expand Up @@ -1130,6 +1141,7 @@ IR_ALWAYS_INLINE void *ir_jit_compile(ir_ctx *ctx, int opt_level, size_t *size)
return NULL;
}
ctx->flags &= ~(IR_OPT_CFG | IR_OPT_CODEGEN | IR_OPT_TAILCALL);
ctx->flags |= IR_NO_OPT;

ir_build_def_use_lists(ctx);

Expand Down
Loading
Loading