Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix char declarations #396

Closed
wants to merge 1 commit into from
Closed

WIP: Fix char declarations #396

wants to merge 1 commit into from

Conversation

j2kun
Copy link
Contributor

@j2kun j2kun commented Apr 3, 2024

Draft. Working notes:

The AST is

CompoundStmt 0x5621e73b38c0
|-DeclStmt 0x5621e73b37c0
| `-VarDecl 0x5621e73b3720  used one 'char' cinit
|   `-ImplicitCastExpr 0x5621e73b37a8 'char' <IntegralCast>
|     `-IntegerLiteral 0x5621e73b3788 'int' 1
`-ReturnStmt 0x5621e73b38b0
  `-ImplicitCastExpr 0x5621e73b3898 'char' <IntegralCast>
    `-BinaryOperator 0x5621e73b3878 'int' '+'
      |-ImplicitCastExpr 0x5621e73b3830 'int' <IntegralCast>
      | `-ImplicitCastExpr 0x5621e73b3818 'char' <LValueToRValue>
      |   `-DeclRefExpr 0x5621e73b37d8 'char' lvalue ParmVar 0x5621e73b3550 'x' 'char'
      `-ImplicitCastExpr 0x5621e73b3860 'int' <IntegralCast>
        `-ImplicitCastExpr 0x5621e73b3848 'char' <LValueToRValue>
          `-DeclRefExpr 0x5621e73b37f8 'char' lvalue Var 0x5621e73b3720 'one' 'char'

Looks like the right place to fix this is in MLIRASTConsumer::getMLIRType in tools/cgeist/Lib/clang-mlir.cc:5825 where it parses the integer type.

Either that or add a separate visitor for ImplicitCastExpr and trunc the arith.constant to the size of the VarDecl.

@j2kun j2kun marked this pull request as draft April 3, 2024 16:07
@j2kun
Copy link
Contributor Author

j2kun commented Apr 3, 2024

Actually, it looks like the problem is in how the AST parses the binary op as a cast to int before a cast back to char.

`-ImplicitCastExpr 'char' <IntegralCast>
    `-BinaryOperator 'int' '+'
      |-ImplicitCastExpr 'int' <IntegralCast>
      | `-ImplicitCastExpr 'char' <LValueToRValue>
      |   `-DeclRefExpr 'char' lvalue ParmVar 'x' 'char'
      `-ImplicitCastExpr 'int' <IntegralCast>
        `-ImplicitCastExpr 'char' <LValueToRValue>
          `-DeclRefExpr 'char' lvalue Var 'one' 'char'

This aligns with the generated MLIR, but how is that semantically correct?

@j2kun
Copy link
Contributor Author

j2kun commented Apr 3, 2024

OK now I'm learning that this is just part of the C standard: all arithmetic is done at int level or higher. Closing.

@j2kun j2kun closed this Apr 3, 2024
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