Skip to content

Commit

Permalink
Add array codegen tests, fix parseType
Browse files Browse the repository at this point in the history
  • Loading branch information
wpmed92 committed Aug 23, 2024
1 parent d7820e0 commit eca395a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/CodeGen/MLIRCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,15 @@ void MLIRCodeGen::visit(ArrayAccessExpression *arrayAccess) {

for (auto &access : arrayAccess->getAccessChain()) {
access->accept(this);
indices.push_back(popExpressionStack().second);
auto val = popExpressionStack().second;

// If it's a variable index, load it first
if (val.getType().isa<spirv::PointerType>()) {
auto loadedIdx = builder.create<spirv::LoadOp>(builder.getUnknownLoc(), val);
indices.push_back(loadedIdx);
} else {
indices.push_back(val);
}
}

Value accessChain = builder.create<spirv::AccessChainOp>(builder.getUnknownLoc(), mlirArray.second, indices);
Expand Down
8 changes: 6 additions & 2 deletions lib/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ std::unique_ptr<Type> Parser::parseType() {
auto qualifiers = parseQualifiers();
auto type = getTypeFromTokenKind(std::move(qualifiers), curToken->getTokenKind());

if (!type) {
return nullptr;
}

std::vector<int> dimensions;

// Parse array type
Expand Down Expand Up @@ -1095,9 +1099,10 @@ std::unique_ptr<ReturnStatement> Parser::parseReturn() {
std::unique_ptr<AssignmentExpression> Parser::parseAssignmentExpression() {
parsingLhsExpression = true;
savePosition();
auto unaryExpression = parseUnaryExpression();

auto unaryExpression = parseUnaryExpression();
advanceToken();

if (!curToken->is(TokenKind::assign)) {
rollbackPosition();
parsingLhsExpression = false;
Expand Down Expand Up @@ -1297,7 +1302,6 @@ std::unique_ptr<Expression> Parser::parsePostfixExpression(bool parsingSubExpres
} else if (auto members = parseMemberAccessChain()) {
return std::make_unique<MemberAccessExpression>(std::move(primary), std::move(*members), parsingLhsExpression);
} else if (auto access = parseArrayAccess()) {
std::cout << "Found array access" << std::endl;
return std::make_unique<ArrayAccessExpression>(std::move(primary), std::move(*access), parsingLhsExpression);
} else {
return primary;
Expand Down
35 changes: 35 additions & 0 deletions test/CodeGen/arrays.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
void main() {
// CHECK: %0 = spirv.CompositeConstruct %cst_f32, %cst_f32_0, %cst_f32_1 : (f32, f32, f32) -> !spirv.array<3 x f32>
float[3] myArray = float[3](0.1, 0.2, 0.3);

// CHECK: %cst0_si32 = spirv.Constant 0 : si32
// CHECK-NEXT: %2 = spirv.AccessChain %1[%cst0_si32] : !spirv.ptr<!spirv.array<3 x f32>, Function>, si32
float a = myArray[0];

// CHECK: %cst1_si32 = spirv.Constant 1 : si32
// CHECK-NEXT: %5 = spirv.AccessChain %1[%cst1_si32] : !spirv.ptr<!spirv.array<3 x f32>, Function>, si32
float b = myArray[1];

// CHECK: %cst2_si32 = spirv.Constant 2 : si32
// CHECK-NEXT: %8 = spirv.AccessChain %1[%cst2_si32] : !spirv.ptr<!spirv.array<3 x f32>, Function>, si32
float c = myArray[2];

// CHECK: %cst0_si32_2 = spirv.Constant 0 : si32
// CHECK-NEXT: %11 = spirv.AccessChain %1[%cst0_si32_2] : !spirv.ptr<!spirv.array<3 x f32>, Function>, si32
// CHECK-NEXT: %cst_f32_3 = spirv.Constant 0.00999999977 : f32
// CHECK-NEXT: spirv.Store "Function" %11, %cst_f32_3 : f32
myArray[0] = 0.01;

// CHECK: %cst1_si32_4 = spirv.Constant 1 : si32
// CHECK-NEXT: %12 = spirv.Variable : !spirv.ptr<si32, Function>
// CHECK-NEXT: spirv.Store "Function" %12, %cst1_si32_4 : si32
int varIdx = 1;

// CHECK: %13 = spirv.Load "Function" %12 : si32
// CHECK-NEXT: %14 = spirv.AccessChain %1[%13] : !spirv.ptr<!spirv.array<3 x f32>, Function>, si32
// CHECK-NEXT: %cst_f32_5 = spirv.Constant 2.000000e-02 : f32
// CHECK-NEXT: spirv.Store "Function" %14, %cst_f32_5 : f32
myArray[varIdx] = 0.02;

return;
}
File renamed without changes.

0 comments on commit eca395a

Please sign in to comment.