microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
2.99k stars 671 forks source link

`out` and `inout` should always be references #5377

Open llvm-beanz opened 1 year ago

llvm-beanz commented 1 year ago

Description All out and inout parameters should use reference types as their qual types in the AST.

DXC's ASTs currently model some types as pass-by-value even when they are out or inout, which makes the ASTs inaccurately reflect the program. In order to address issues with AST-based error and warning analysis and implement a correct calling convention for DXIL libraries we need to produce accurate ASTs.

Steps to Reproduce Example shader

See the produced AST:

TranslationUnitDecl 0x55e2317384a0 <<invalid sloc>> <invalid sloc>
|-CXXRecordDecl 0x55e2317668c0 <<source>:1:1, line:3:1> line:1:8 referenced struct SomeStruct definition
| |-CXXRecordDecl 0x55e231766a00 <col:1, col:8> col:8 implicit struct SomeStruct
| `-FieldDecl 0x55e231766ac8 <line:2:3, col:7> col:7 SomeVal 'int'
|-FunctionDecl 0x55e2317741b0 <line:5:1, line:6:1> line:5:6 used Fn 'void (__restrict SomeStruct)'
| |-ParmVarDecl 0x55e231766b30 <col:9, col:26> col:26 S '__restrict SomeStruct'
| | `-HLSLInOutAttr 0x55e231766ba0 <col:9>
| `-CompoundStmt 0x55e2317742c8 <col:29, line:6:1>
`-FunctionDecl 0x55e231774300 <line:8:1, line:11:1> line:8:6 Fn2 'void ()'
  `-CompoundStmt 0x55e231774620 <col:12, line:11:1>
    |-DeclStmt 0x55e231774450 <line:9:3, col:15>
    | `-VarDecl 0x55e2317743e0 <col:3, col:14> col:14 used S 'SomeStruct'
    `-CallExpr 0x55e2317745c0 <line:10:3, col:7> 'void'
      |-ImplicitCastExpr 0x55e2317745a8 <col:3> 'void (*)(__restrict SomeStruct)' <FunctionToPointerDecay>
      | `-DeclRefExpr 0x55e231774528 <col:3> 'void (__restrict SomeStruct)' lvalue Function 0x55e2317741b0 'Fn' 'void (__restrict SomeStruct)'
      `-ImplicitCastExpr 0x55e231774608 <col:6> 'SomeStruct' <LValueToRValue>
        `-DeclRefExpr 0x55e2317744c0 <col:6> 'SomeStruct' lvalue Var 0x55e2317743e0 'S' 'SomeStruct'

FunctionDecl 0x55e2317741b0 <line:5:1, line:6:1> line:5:6 used Fn 'void (__restrict SomeStruct)' should have a reference parameter.

The argument copy semantics are also not represented in the AST, which will need to be fixed to address this.

Environment

llvm-beanz commented 1 year ago

A draft PR for this is posted as #5249