Skip to content

Refactor AST to Enum-based & Implement OpenACC 3.4 'indirect' Clause#15

Merged
ouankou merged 6 commits intomainfrom
feature/openacc-3.4-indirect-refactor
Dec 22, 2025
Merged

Refactor AST to Enum-based & Implement OpenACC 3.4 'indirect' Clause#15
ouankou merged 6 commits intomainfrom
feature/openacc-3.4-indirect-refactor

Conversation

@ouankou
Copy link
Owner

@ouankou ouankou commented Dec 21, 2025

This PR refactors the AST to remove string-based hacks in favor of enum-based logic and implements full support for the OpenACC 3.4 'indirect' clause.

Key Changes

  • AST Refactoring: Replaced string-based original_keyword in data clauses with OpenACCDataClauseVariant enums.
  • OpenACC 3.4 Support: Implemented indirect clause for routine directive, supporting both indirect and indirect(name).
  • Bug Fixes: Resolved a critical segmentation fault in exitName_or_string by correcting unsafe casting logic in OpenACCASTConstructor.
  • Parsing: Updated acclexer.g4 and accparser.g4 to support the new token and rules.
  • Testing: Added new test case tests/builtin/indirect.txt.

Verification

  • Test Suite: Passed all 915 tests (ctest).
  • Formatting: Verified that code formatting changes do not affect functionality.

@gemini-code-assist
Copy link

Summary of Changes

Hello @ouankou, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the Abstract Syntax Tree (AST) to improve its internal representation of OpenACC clauses. By transitioning from string-based keyword identification to a more structured enum-based approach, the system gains better type safety and reduces the potential for errors. Concurrently, it introduces full compliance for the OpenACC 3.4 indirect clause, expanding the supported feature set. A critical bug causing a segmentation fault during AST construction has also been addressed, contributing to overall stability.

Highlights

  • AST Refactoring: Replaced string-based original_keyword fields in data clauses with robust enum-based OpenACCDataClauseVariant logic, enhancing type safety and maintainability.
  • OpenACC 3.4 'indirect' Clause Support: Implemented full support for the OpenACC 3.4 indirect clause for the routine directive, including both indirect and indirect(name) forms.
  • Bug Fixes: Resolved a critical segmentation fault in exitName_or_string by correcting unsafe casting logic within the OpenACCASTConstructor.
  • Parsing Enhancements: Updated acclexer.g4 and accparser.g4 to correctly parse new tokens and rules, including support for data clause modifiers (e.g., always, readonly) and keyword arguments for gang clauses.
  • Testing: Added a new test case tests/builtin/indirect.txt to verify the correct parsing and representation of the indirect clause.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ouankou
Copy link
Owner Author

ouankou commented Dec 21, 2025

@codex review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces significant refactoring and new feature support for OpenACC clause parsing and representation. Key changes include adding support for the indirect clause, enhancing the parsing of gang clause arguments, and improving the handling of device_type items. The parsing logic for bind, collapse, default, and reduction clauses has been updated to use direct context checks instead of string comparisons, leading to more robust and efficient parsing. A new OpenACCDataClause base class and addModifier method were introduced to consolidate the handling of data clause modifiers (e.g., always, readonly, zero) across copy, copyin, copyout, and create clauses, replacing older, less flexible modifier mechanisms. Correspondingly, the OpenACCIR.h file was updated to reflect these new class hierarchies and removed redundant modifier-specific functions. The OpenACCIRToString.cpp file was adjusted to correctly print the new indirect clause, collapse clause with force modifier, and gang arguments, and to use * for any device type. The lexer and parser grammars (acclexer.g4, accparser.g4) were extensively modified to support these new syntax elements and parsing strategies, including new lexer modes for bind, collapse, copy, copyin, copyout, create, and device_type clauses, and new tokens for data clause modifiers. A new test file indirect.txt and its reference output were added. Review comments highlighted the need to strip quotes from string literals in the indirect clause for correct output, suggested using static_cast for type safety, and recommended simplifying repeated code for extracting gang argument values. A memory leak fix was noted for deleting merged clauses.

Comment on lines 1027 to 1030
std::string text =
trimEnclosingWhiteSpace(ctx->name_or_string()->getText());
bool is_str = ctx->name_or_string()->STRING_LITERAL() != nullptr;
indirect->setValue(text, is_str);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When a string literal is parsed, ctx->name_or_string()->getText() includes the enclosing quotes. Storing the value with quotes and then adding another pair of quotes in OpenACCIndirectClause::toString() will result in incorrect output (e.g., ""foo""). The enclosing quotes should be stripped here before setting the value. This will ensure the toString() method can correctly reconstruct the pragma.

    std::string text =
        trimEnclosingWhiteSpace(ctx->name_or_string()->getText());
    bool is_str = ctx->name_or_string()->STRING_LITERAL() != nullptr;
    if (is_str && text.length() >= 2) {
      if ((text.front() == '"' && text.back() == '"') || (text.front() == '\'' && text.back() == '\'')) {
        text = text.substr(1, text.length() - 2);
      }
    }
    indirect->setValue(text, is_str);

Comment on lines 499 to 517
if (ctx->NUM()) {
kind = ACCC_GANG_ARG_num;
if (ctx->int_expr()) {
value_text = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
}
} else if (ctx->DIM()) {
kind = ACCC_GANG_ARG_dim;
if (ctx->int_expr()) {
value_text = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
}
} else if (ctx->STATIC()) {
kind = ACCC_GANG_ARG_static;
if (ctx->int_expr()) {
value_text = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
}
} else if (ctx->int_expr()) {
kind = ACCC_GANG_ARG_num_no_keyword;
value_text = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to extract value_text from ctx->int_expr() is repeated multiple times within the if-else if chain. This can be simplified by extracting the expression text once at the beginning, which improves readability and reduces code duplication.

  if (ctx->int_expr()) {
    value_text = trimEnclosingWhiteSpace(ctx->int_expr()->getText());
  }

  if (ctx->NUM()) {
    kind = ACCC_GANG_ARG_num;
  } else if (ctx->DIM()) {
    kind = ACCC_GANG_ARG_dim;
  } else if (ctx->STATIC()) {
    kind = ACCC_GANG_ARG_static;
  } else if (ctx->int_expr()) {
    kind = ACCC_GANG_ARG_num_no_keyword;
  }

mergeVarList(existing, incoming);
current_clauses->pop_back();
directive->getClausesInOriginalOrder()->pop_back();
delete incoming;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's great that you're deleting the incoming clause object after merging to prevent a memory leak. This is a critical fix for correctness.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ouankou
Copy link
Owner Author

ouankou commented Dec 21, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 1021 to 1024
std::string text =
trimEnclosingWhiteSpace(ctx->name_or_string()->getText());
bool is_str = ctx->name_or_string()->STRING_LITERAL() != nullptr;
if (is_str && text.length() >= 2) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat indirect string literals as non-strings

The new indirect clause relies on STRING_LITERAL to decide whether to strip quotes (is_str = ctx->name_or_string()->STRING_LITERAL() != nullptr), but the lexer only defines STRING_LITERAL in bind_clause mode (see acclexer.g4 around mode bind_clause, while INDIRECT pushes expr_clause at acclexer.g4 lines ~361–366). As a result, #pragma acc routine indirect("foo") is tokenized as EXPR, is_str stays false, and the AST keeps the quoted text as a non-string value. This changes clause semantics for valid string-literal forms. Consider adding STRING_LITERAL to expr_clause/expression_mode or falling back to quote-detection for EXPR in this path.

Useful? React with 👍 / 👎.

@ouankou
Copy link
Owner Author

ouankou commented Dec 21, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ouankou
Copy link
Owner Author

ouankou commented Dec 21, 2025

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 579 to 582
data_clause_in_modifier_list =
lookAheadToken("always") || lookAheadToken("alwaysin") || lookAheadToken("alwaysout") ||
lookAheadToken("capture") || lookAheadToken("readonly") || lookAheadToken("zero");
if (!data_clause_in_modifier_list) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat keyword-named vars as modifiers in data clauses

The modifier detection now uses lookAheadToken(...) to decide whether the text after copy(/copyin(/etc. is a modifier list. That means identifiers that exactly match modifier keywords (e.g., always, alwaysin, alwaysout, capture, readonly, zero) are no longer parseable as variable names because the lexer assumes a modifier list and the parser then requires a colon. For example, #pragma acc data copy(always) (a valid variable name in C/Fortran) will now be rejected as a syntax error. This is a regression for any code that uses identifiers equal to those keywords.

Useful? React with 👍 / 👎.

@ouankou
Copy link
Owner Author

ouankou commented Dec 21, 2025

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ouankou ouankou merged commit 6d7fcdc into main Dec 22, 2025
11 checks passed
@ouankou ouankou deleted the feature/openacc-3.4-indirect-refactor branch December 22, 2025 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant