sorry, the title information is not very specific
, below is my example:
const vec3 a = vec3(1.0, 2.0, 3.0);
const float b = a.x;
void main() {
}
we have an opengl compiler based on LLVM, vec3 is a vector type containing 3 float numbers, as you can seeļ¼ a is a constant value, a.x should be constant value too, it should be 1.0, but when we compile this code, we get below IR:
@a = dso_local addrspace(1) global <3 x float> <float 1.000000e+00, float 2.000000e+00, float 3.000000e+00>, align 16, !dbg !31
@b = dso_local addrspace(1) global float 0.000000e+00, align 4, !dbg !37
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_example.vert, ptr null }]
define internal void @__cxx_global_var_init() #0 !dbg !44 {
entry:
%0 = load <3 x float>, ptr addrspace(1) @a, align 16, !dbg !47
%1 = extractelement <3 x float> %0, i32 0, !dbg !47
store float %1, ptr addrspacecast (ptr addrspace(1) @b to ptr), align 4, !dbg !48
ret void, !dbg !47
}
define dso_local void @vert_main() #1 !dbg !49 {
entry:
store float 1.000000e+00, ptr addrspace(1) @gl_PointSize, align 4
store <4 x float> zeroinitializer, ptr addrspace(1) @gl_Position, align 16
ret void, !dbg !51
}
Unexpected __cxx_global_var_init function` was generated, below is related AST dump:
|-VarDecl 0x5c389caeab00 </home/triton/work/egp-llvm/build_Debug/lib/clang/17/include/vert.h:4:1, col:45> col:12 gl_Position 'float __attribute__((essl_vector_type(4)))' cinit
| `-InitListExpr 0x5c389caeac90 <col:26, col:45> 'float __attribute__((essl_vector_type(4)))'
| |-FloatingLiteral 0x5c389caeab70 <col:27> 'float' 0.000000e+00
| |-FloatingLiteral 0x5c389caeab98 <col:32> 'float' 0.000000e+00
| |-FloatingLiteral 0x5c389caeabc0 <col:37> 'float' 0.000000e+00
| `-FloatingLiteral 0x5c389caeabe8 <col:42> 'float' 0.000000e+00
|-VarDecl 0x5c389caead18 <line:5:1, col:30> col:15 gl_PointSize 'float' cinit
| `-FloatingLiteral 0x5c389caead88 <col:30> 'float' 1.000000e+00
|-VarDecl 0x5c389caeadd0 <<source>:1:1, col:28> col:6 used a 'float __attribute__((essl_vector_type(3)))' cinit
| `-CompoundLiteralExpr 0x5c389caeb068 <col:10, col:28> 'float __attribute__((essl_vector_type(3)))'
| `-InitListExpr 0x5c389caeafa8 <col:14, col:28> 'float __attribute__((essl_vector_type(3)))'
| |-ConstantExpr 0x5c389caeb008 <col:15> 'float'
| | `-FloatingLiteral 0x5c389caeae60 <col:15> 'float' 1.000000e+00
| |-ConstantExpr 0x5c389caeb028 <col:20> 'float'
| | `-FloatingLiteral 0x5c389caeae88 <col:20> 'float' 2.000000e+00
| `-ConstantExpr 0x5c389caeb048 <col:25> 'float'
| `-FloatingLiteral 0x5c389caeaeb0 <col:25> 'float' 3.000000e+00
|-VarDecl 0x5c389caeb0b8 <line:2:1, col:13> col:7 b 'float' cinit
| `-ImplicitCastExpr 0x5c389caeb180 <col:11, col:13> 'float' <LValueToRValue>
| `-ESSLVectorComponentExpr 0x5c389caeb150 <col:11, col:13> 'float' lvalue vectorcomponent x
| `-DeclRefExpr 0x5c389caeb128 <col:11> 'float __attribute__((essl_vector_type(3)))' lvalue Var 0x5c389caeadd0 'a' 'float __attribute__((essl_vector_type(3)))'
`-FunctionDecl 0x5c389caeb200 <line:3:1, col:14> col:6 vert_main 'void (void)'
`-CompoundStmt 0x5c389caeb388 <col:13, col:14>
so we add a custom function like VisitExtVectorComponentExpr
bool LValueExprEvaluator::VisitESSLVectorComponentExpr(const ESSLVectorComponentExpr *E) {
if (!Visit(E->getBase()))
return false;
// č·ååéē»ä»¶č®æ
SmallVector<uint32_t, 4> Indices;
E->getEncodedComponentAccess(Indices);
APValue BaseVal;
if (!handleLValueToRValueConversion(Info, E->getBase(),
E->getBase()->getType(), Result, BaseVal))
return false;
if (BaseVal.isVector()) {
if (Indices.size() == 1) {
BaseVal.dump();
Result.moveInto(BaseVal.getVectorElt(Indices[0]));
// Result.set(BaseVal.getVectorElt(Indices[0]).getLValueBase());
return true;
} else {
SmallVector<APValue, 4> Elements;
for (unsigned I = 0; I < Indices.size(); ++I) {
Elements.push_back(BaseVal.getVectorElt(Indices[I]));
}
APValue VecResult(Elements.data(), Indices.size());
Result.setFrom(Info.Ctx, VecResult);
return true;
}
}
return false;
}
but it doesnāt work, any suggestion or idea was appreciatedā¤ļø