Skip to content

Commit e42fa19

Browse files
authored
Merge pull request #114 from Shopify/yjit-dup-comments
YJIT: Avoid adding duplicate code comments
1 parent 2e326c9 commit e42fa19

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

yjit_codegen.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,25 @@ _counted_side_exit(uint8_t *existing_side_exit, int64_t *counter)
217217
return start;
218218
}
219219

220+
// Add a comment at the current position in the code block
221+
static void
222+
_add_comment(codeblock_t* cb, const char* comment_str)
223+
{
224+
// Avoid adding duplicate comment strings (can happen due to deferred codegen)
225+
size_t num_comments = rb_darray_size(yjit_code_comments);
226+
if (num_comments > 0) {
227+
struct yjit_comment last_comment = rb_darray_get(yjit_code_comments, num_comments - 1);
228+
if (last_comment.offset == cb->write_pos && strcmp(last_comment.comment, comment_str) == 0) {
229+
return;
230+
}
231+
}
232+
233+
struct yjit_comment new_comment = (struct yjit_comment){ cb->write_pos, comment_str };
234+
rb_darray_append(&yjit_code_comments, new_comment);
235+
}
236+
220237
// Comments for generated machine code
221-
#define ADD_COMMENT(cb, comment) rb_darray_append(&yjit_code_comments, ((struct yjit_comment){(cb)->write_pos, (comment)}))
238+
#define ADD_COMMENT(cb, comment) _add_comment((cb), (comment))
222239
yjit_comment_array_t yjit_code_comments;
223240

224241
#else

yjit_iface.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ YJIT_DECLARE_COUNTERS(
7070
#undef YJIT_DECLARE_COUNTERS
7171

7272
struct yjit_comment {
73-
int32_t offset;
73+
uint32_t offset;
7474
const char *comment;
7575
};
7676

0 commit comments

Comments
 (0)