From a5fac4e084d47bc1cd8cae00f142c08b30b162da Mon Sep 17 00:00:00 2001 From: Caio Oliveira Date: Thu, 15 Jan 2026 14:15:09 -0800 Subject: [PATCH] brw: Create a struct to hold parser state Hold most of the parser data. Remaining will be moved in follow-up patches. The struct itself is still a global for now. Reviewed-by: Sagar Ghuge Part-of: --- src/intel/compiler/brw/brw_asm.c | 34 ++++++++++------------- src/intel/compiler/brw/brw_asm_internal.h | 16 +++++++++-- src/intel/compiler/brw/brw_gram.y | 14 +++++----- src/intel/compiler/brw/brw_lex.l | 14 ++++------ 4 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/intel/compiler/brw/brw_asm.c b/src/intel/compiler/brw/brw_asm.c index 40025d103bd..efce5497a49 100644 --- a/src/intel/compiler/brw/brw_asm.c +++ b/src/intel/compiler/brw/brw_asm.c @@ -13,15 +13,8 @@ extern FILE *yyin; +struct brw_asm_parser *parser; struct brw_codegen *p; -const char *input_filename; -int errors; -bool compaction_warning_given; - -/* - * Label tracking. - */ -static struct hash_table *brw_asm_labels; typedef struct { char *name; @@ -35,15 +28,15 @@ brw_asm_label_lookup(const char *name) { uint32_t h = _mesa_hash_string(name); struct hash_entry *entry = - _mesa_hash_table_search_pre_hashed(brw_asm_labels, h, name); + _mesa_hash_table_search_pre_hashed(parser->labels, h, name); if (!entry) { - void *mem_ctx = brw_asm_labels; + void *mem_ctx = parser->labels; brw_asm_label *label = rzalloc(mem_ctx, brw_asm_label); label->name = ralloc_strdup(mem_ctx, name); label->offset = -1; util_dynarray_init(&label->jip_uses, mem_ctx); util_dynarray_init(&label->uip_uses, mem_ctx); - entry = _mesa_hash_table_insert_pre_hashed(brw_asm_labels, + entry = _mesa_hash_table_insert_pre_hashed(parser->labels, h, name, label); } assert(entry); @@ -83,7 +76,7 @@ brw_postprocess_labels() unsigned unknown = 0; void *store = p->store; - hash_table_foreach(brw_asm_labels, entry) { + hash_table_foreach(parser->labels, entry) { brw_asm_label *label = entry->data; if (label->offset == -1) { @@ -114,8 +107,10 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo, { brw_assemble_result result = {0}; - void *tmp_ctx = ralloc_context(mem_ctx); - brw_asm_labels = _mesa_string_hash_table_create(tmp_ctx); + assert(parser == NULL); + parser = rzalloc(mem_ctx, brw_asm_parser); + parser->devinfo = devinfo; + parser->labels = _mesa_string_hash_table_create(parser); struct brw_isa_info isa; brw_init_isa_info(&isa, devinfo); @@ -124,11 +119,11 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo, brw_init_codegen(&isa, p, p); yyin = f; - input_filename = filename; + parser->input_filename = filename; - compaction_warning_given = false; + parser->compaction_warning_given = false; int err = yyparse(); - if (err || errors) + if (err || parser->errors) goto end; if (!brw_postprocess_labels()) @@ -167,11 +162,10 @@ brw_assemble(void *mem_ctx, const struct intel_device_info *devinfo, end: /* Reset internal state. */ yyin = NULL; - input_filename = NULL; p = NULL; - brw_asm_labels = NULL; - ralloc_free(tmp_ctx); + ralloc_free(parser); + parser = NULL; return result; } diff --git a/src/intel/compiler/brw/brw_asm_internal.h b/src/intel/compiler/brw/brw_asm_internal.h index ce02fa879c8..de8cbb070d4 100644 --- a/src/intel/compiler/brw/brw_asm_internal.h +++ b/src/intel/compiler/brw/brw_asm_internal.h @@ -22,14 +22,24 @@ /* glibc < 2.27 defines OVERFLOW in /usr/include/math.h. */ #undef OVERFLOW +typedef struct brw_asm_parser { + const struct intel_device_info *devinfo; + const char *input_filename; + int errors; + bool compaction_warning_given; + struct hash_table *labels; + + /* Lexer state. */ + int yycolumn; + int saved_state; +} brw_asm_parser; + int yyparse(void); int yylex(void); char *lex_text(void); +extern struct brw_asm_parser *parser; extern struct brw_codegen *p; -extern int errors; -extern bool compaction_warning_given; -extern const char *input_filename; struct condition { unsigned cond_modifier:4; diff --git a/src/intel/compiler/brw/brw_gram.y b/src/intel/compiler/brw/brw_gram.y index 781db7f2f99..d28312acffe 100644 --- a/src/intel/compiler/brw/brw_gram.y +++ b/src/intel/compiler/brw/brw_gram.y @@ -62,11 +62,11 @@ message(enum message_level level, YYLTYPE *location, va_list args; if (location) - fprintf(stderr, "%s:%d:%d: %s: ", input_filename, + fprintf(stderr, "%s:%d:%d: %s: ", parser->input_filename, location->first_line, location->first_column, level_str[level]); else - fprintf(stderr, "%s:%s: ", input_filename, level_str[level]); + fprintf(stderr, "%s:%s: ", parser->input_filename, level_str[level]); va_start(args, fmt); vfprintf(stderr, fmt, args); @@ -606,11 +606,11 @@ add_instruction_option(struct options *options, struct instoption opt) * assemble things later will set the flag if it decides to * compact instructions. */ - if (!compaction_warning_given) { - compaction_warning_given = true; + if (!parser->compaction_warning_given) { + parser->compaction_warning_given = true; fprintf(stderr, "%s: ignoring 'compacted' " "annotations for text assembly " - "instructions\n", input_filename); + "instructions\n", parser->input_filename); } break; case ACCWREN: @@ -2228,6 +2228,6 @@ yyerror(char *msg) #endif { fprintf(stderr, "%s: %d: %s at \"%s\"\n", - input_filename, yylineno, msg, lex_text()); - ++errors; + parser->input_filename, yylineno, msg, lex_text()); + ++parser->errors; } diff --git a/src/intel/compiler/brw/brw_lex.l b/src/intel/compiler/brw/brw_lex.l index d711edc9387..13824fcffca 100644 --- a/src/intel/compiler/brw/brw_lex.l +++ b/src/intel/compiler/brw/brw_lex.l @@ -6,18 +6,16 @@ #undef ALIGN16 #include "brw_gram.tab.h" -/* Locations */ int yycolumn = 1; -int saved_state = 0; -extern const char *input_filename; - #define YY_NO_INPUT + #define YY_USER_ACTION \ yylloc.first_line = yylloc.last_line = yylineno; \ yylloc.first_column = yycolumn; \ yylloc.last_column = yycolumn + yyleng - 1; \ yycolumn += yyleng; + %} %x BLOCK_COMMENT @@ -33,9 +31,9 @@ extern const char *input_filename; \/\/.*[\r\n] { yycolumn = 1; } /* eat up multiline comment */ -\/\* { saved_state = YYSTATE; BEGIN(BLOCK_COMMENT); } +\/\* { parser->saved_state = YYSTATE; BEGIN(BLOCK_COMMENT); } -\*\/ { BEGIN(saved_state); } +\*\/ { BEGIN(parser->saved_state); } . { } [\r\n] { } @@ -44,7 +42,7 @@ extern const char *input_filename; char *name = malloc(yyleng - 1); memmove(name, yytext + 1, yyleng - 2); name[yyleng-1] = '\0'; - input_filename = name; + parser->input_filename = name; } /* null register */ @@ -435,7 +433,7 @@ sr[0-9]+ { yylval.integer = atoi(yytext + 2); return STATEREG; } . { fprintf(stderr, "%s: %d: %s: at \"%s\"\n", - input_filename, yylineno, + parser->input_filename, yylineno, "unexpected token", lex_text()); } %%