@@ -989,7 +989,7 @@ static int codelocs_nstmts(jl_string_t *cl) JL_NOTSAFEPOINT
989989
990990#define IR_DATASIZE_FLAGS sizeof(uint16_t)
991991#define IR_DATASIZE_PURITY sizeof(uint16_t)
992- #define IR_DATASIZE_INLINING_COST sizeof(uint16_t )
992+ #define IR_DATASIZE_INLINING_COST sizeof(uint8_t )
993993#define IR_DATASIZE_NSLOTS sizeof(int32_t)
994994typedef enum {
995995 ir_offset_flags = 0 ,
@@ -1044,7 +1044,7 @@ JL_DLLEXPORT jl_string_t *jl_compress_ir(jl_method_t *m, jl_code_info_t *code)
10441044 code -> ssaflags );
10451045 write_uint16 (s .s , checked_size (flags .packed , IR_DATASIZE_FLAGS ));
10461046 write_uint16 (s .s , checked_size (code -> purity .bits , IR_DATASIZE_PURITY ));
1047- write_uint16 (s .s , checked_size (code -> inlining_cost , IR_DATASIZE_INLINING_COST ));
1047+ write_uint8 (s .s , checked_size (jl_encode_inlining_cost ( code -> inlining_cost ) , IR_DATASIZE_INLINING_COST ));
10481048
10491049 size_t nslots = jl_array_nrows (code -> slotflags );
10501050 assert (nslots >= m -> nargs && nslots < INT32_MAX ); // required by generated functions
@@ -1109,6 +1109,8 @@ JL_DLLEXPORT jl_code_info_t *jl_uncompress_ir(jl_method_t *m, jl_code_instance_t
11091109{
11101110 if (jl_is_code_info (data ))
11111111 return (jl_code_info_t * )data ;
1112+ if (!jl_is_string (data ))
1113+ return (jl_code_info_t * )jl_nothing ;
11121114 JL_TIMING (AST_UNCOMPRESS , AST_UNCOMPRESS );
11131115 JL_LOCK (& m -> writelock ); // protect the roots array (Might GC)
11141116 assert (jl_is_method (m ));
@@ -1139,7 +1141,7 @@ JL_DLLEXPORT jl_code_info_t *jl_uncompress_ir(jl_method_t *m, jl_code_instance_t
11391141 code -> nospecializeinfer = flags .bits .nospecializeinfer ;
11401142 code -> isva = flags .bits .isva ;
11411143 code -> purity .bits = read_uint16 (s .s );
1142- code -> inlining_cost = read_uint16 ( s .s );
1144+ code -> inlining_cost = jl_decode_inlining_cost ( read_uint8 ( s .s ) );
11431145
11441146 size_t nslots = read_int32 (s .s );
11451147 code -> slotflags = jl_alloc_array_1d (jl_array_uint8_type , nslots );
@@ -1240,12 +1242,46 @@ JL_DLLEXPORT uint8_t jl_ir_flag_has_image_globalref(jl_string_t *data)
12401242 return flags .bits .has_image_globalref ;
12411243}
12421244
1243- JL_DLLEXPORT uint16_t jl_ir_inlining_cost (jl_string_t * data )
1245+ // create a compressed u16 value with range 0..3968, 3 bits exponent, 5 bits mantissa, implicit first digit, rounding up, full accuracy over 0..63
1246+ JL_DLLEXPORT uint8_t jl_encode_inlining_cost (uint16_t inlining_cost )
12441247{
1248+ unsigned shift = 0 ;
1249+ unsigned mantissa ;
1250+ if (inlining_cost <= 0x1f ) {
1251+ mantissa = inlining_cost ;
1252+ }
1253+ else {
1254+ while (inlining_cost >> 5 >> shift != 0 )
1255+ shift ++ ;
1256+ assert (1 <= shift && shift <= 11 );
1257+ mantissa = (inlining_cost >> (shift - 1 )) & 0x1f ;
1258+ mantissa += (inlining_cost & ((1 << (shift - 1 )) - 1 )) != 0 ; // round up if trailing bits non-zero, overflowing into exp
1259+ }
1260+ unsigned r = (shift << 5 ) + mantissa ;
1261+ if (r > 0xff )
1262+ r = 0xff ;
1263+ return r ;
1264+ }
1265+
1266+ JL_DLLEXPORT uint16_t jl_decode_inlining_cost (uint8_t inlining_cost )
1267+ {
1268+ unsigned shift = inlining_cost >> 5 ;
1269+ if (inlining_cost == 0xff )
1270+ return 0xffff ;
1271+ else if (shift == 0 )
1272+ return inlining_cost ;
1273+ else
1274+ return (0x20 | (inlining_cost & 0x1f )) << (shift - 1 );
1275+ }
1276+
1277+ JL_DLLEXPORT uint16_t jl_ir_inlining_cost (jl_value_t * data )
1278+ {
1279+ if (jl_is_uint8 (data ))
1280+ return jl_decode_inlining_cost (* (uint8_t * )data );
12451281 if (jl_is_code_info (data ))
12461282 return ((jl_code_info_t * )data )-> inlining_cost ;
12471283 assert (jl_is_string (data ));
1248- uint16_t res = jl_load_unaligned_i16 ( jl_string_data (data ) + ir_offset_inlining_cost );
1284+ uint16_t res = jl_decode_inlining_cost ( * ( uint8_t * )( jl_string_data (data ) + ir_offset_inlining_cost ) );
12491285 return res ;
12501286}
12511287
0 commit comments