Skip to content

Commit

Permalink
Fixed some parallelism issues on for statement (#69 from wesuRage/main)
Browse files Browse the repository at this point in the history
Fixed some parallelism issues on for statement
  • Loading branch information
wesuRage authored Dec 22, 2024
2 parents 7fa3595 + 95765ac commit 6eaa769
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 48 deletions.
11 changes: 5 additions & 6 deletions examples/a.glx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
extern int writeln( string) ;
extern int writeln( string) ;
extern string itos( int) ;

def main( ) -> int:
def main( ) -> int:

int N := 100000;

for parallel static ( int i := 0; i < N; ++ i) -> 8:
writeln( "hello") ;
for ( int i := 0; i < 100000; ++ i) :
writeln( "val" ) ;
end;

return 0;
Expand Down
8 changes: 8 additions & 0 deletions include/backend/generator/utils/return_id.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GLOBAL_ID_RETURN_H
#define GLOBAL_ID_RETURN_H

#include <string>

extern std::string global_id_return;

#endif // GLOBAL_ID_RETURN_H
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "backend/generator/expressions/generate_assignment_expr.hpp"
#include "backend/generator/expressions/generate_expr.hpp"
#include "backend/generator/utils/return_id.hpp"

llvm::Value *generate_assignment_expr(AssignmentNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
// Evaluate the right-hand side expression and get its LLVM value representation.
llvm::Value *right_value = generate_expr(node->value, Context, Builder, Module);

// Evaluate the left-hand side expression to get the memory location (pointer).
global_id_return = "declaration";
llvm::Value *left_value = generate_expr(node->left, Context, Builder, Module);

// Evaluate the right-hand side expression and get its LLVM value representation.
global_id_return = "value";
llvm::Value *right_value = generate_expr(node->value, Context, Builder, Module);

// Create a store instruction that assigns the value of the
// right-hand side to the memory location of the left-hand side.
Builder.CreateStore(right_value, left_value);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/generator/expressions/generate_call.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "backend/generator/expressions/generate_call.hpp"
#include "backend/generator/expressions/generate_expr.hpp"
#include "backend/generator/symbols/function_symbol_table.hpp"
#include "backend/generator/parallel/queue.hpp"
#include "backend/generator/utils/return_id.hpp"

llvm::Value *generate_call(CallNode *call_node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
if (!call_node || !call_node->caller) {
Expand Down
16 changes: 9 additions & 7 deletions src/backend/generator/expressions/generate_identifier.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "backend/generator/expressions/generate_identifier.hpp"
#include "backend/generator/symbols/identifier_symbol_table.hpp"
#include "backend/generator/parallel/queue.hpp"
#include "backend/generator/utils/return_id.hpp"

llvm::Value *generate_identifier(IdentifierNode *node) {
try {
return find_or_wait_for_identifier(node, global_id_return);
} catch (...) {
throw std::runtime_error("Error: identifier not found!");
const SymbolInfo* id = find_identifier(node->symbol);
if (id) {
if (global_id_return == "declaration") {
return id->declaration;
} else {
return id->value;
}
}
}

}
4 changes: 0 additions & 4 deletions src/backend/generator/generator.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "backend/generator/generate_ir.hpp"
#include "backend/generator/symbols/symbol_stack.hpp"
#include "backend/generator/parallel/queue.hpp"

extern "C" {
#include "frontend/lexer/core.h"
Expand Down Expand Up @@ -85,9 +84,6 @@ int main(int argc, char **argv) {
// Create the global scope
enter_scope();

std::thread pendingThread(process_pending_identifiers_periodically);
pendingThread.detach();

std::vector<llvm::Value*> values = generate_ir(ast, TheContext, TheModule, Builder);

// Print out the generated LLVM IR for debugging
Expand Down
7 changes: 2 additions & 5 deletions src/backend/generator/statements/generate_extern_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "backend/generator/types/generate_type.hpp"
#include "backend/generator/symbols/function_symbol_table.hpp"
#include "backend/generator/symbols/identifier_symbol_table.hpp"
#include "backend/generator/parallel/queue.hpp"
#include "backend/generator/utils/return_id.hpp"

llvm::Value* generate_extern_stmt(ExternNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
if (!node || !node->type || !node->identifier) {
Expand Down Expand Up @@ -45,10 +45,7 @@ llvm::Value* generate_extern_stmt(ExternNode *node, llvm::LLVMContext &Context,

global_var->setLinkage(llvm::GlobalValue::ExternalLinkage);

{
std::unique_lock<std::mutex> lock(symbolTableMutex);
add_identifier(node->identifier, global_var, nullptr, decl_type);
}
add_identifier(node->identifier, global_var, nullptr, decl_type);

return global_var;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/backend/generator/statements/generate_for_stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ llvm::Value* generate_for_stmt(ForNode *node, llvm::LLVMContext &Context, llvm::
}

if (stopVal->getType() != llvm::Type::getInt32Ty(Context)) {
stopVal = Builder.CreateLoad(llvm::Type::getInt32Ty(Context), stopVal, "stopval");
stopVal = Builder.CreateSExt(stopVal, llvm::Type::getInt32Ty(Context));
}

Expand Down
38 changes: 19 additions & 19 deletions src/backend/generator/statements/generate_outlined_for.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,6 @@ llvm::Function* generate_outlined_for(ForNode *node, llvm::LLVMContext &Context,
Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 1), ompStride); // Incremento
Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Context), 0), ompIsLast); // Última iteração (inicialmente 0)

llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", outlinedFunc);
llvm::BasicBlock *condBB = llvm::BasicBlock::Create(Context, "cond", outlinedFunc);
llvm::BasicBlock *bodyBB = llvm::BasicBlock::Create(Context, "body", outlinedFunc);
llvm::BasicBlock *updateBB = llvm::BasicBlock::Create(Context, "update", outlinedFunc);
llvm::BasicBlock *endBB = llvm::BasicBlock::Create(Context, "endloop", outlinedFunc);

Builder.CreateBr(preLoopBB);
Builder.SetInsertPoint(preLoopBB);

Builder.CreateBr(condBB);

Builder.SetInsertPoint(condBB);
llvm::Value *loopVarValLoad = Builder.CreateLoad(loopVarVal->getType(), loopVar, node->variable);
llvm::Value *cond = Builder.CreateICmpSLT(loopVarValLoad, stopVal, "loopcond");

Builder.CreateCondBr(cond, bodyBB, endBB);

Builder.SetInsertPoint(bodyBB);

// Determinar o tipo de agendamento com base na política
llvm::Value *scheduleType = nullptr;
if (strcmp(schedule_policy, "static") == 0) {
Expand Down Expand Up @@ -146,6 +127,25 @@ llvm::Function* generate_outlined_for(ForNode *node, llvm::LLVMContext &Context,
});
}

llvm::BasicBlock *preLoopBB = llvm::BasicBlock::Create(Context, "preloop", outlinedFunc);
llvm::BasicBlock *condBB = llvm::BasicBlock::Create(Context, "cond", outlinedFunc);
llvm::BasicBlock *bodyBB = llvm::BasicBlock::Create(Context, "body", outlinedFunc);
llvm::BasicBlock *updateBB = llvm::BasicBlock::Create(Context, "update", outlinedFunc);
llvm::BasicBlock *endBB = llvm::BasicBlock::Create(Context, "endloop", outlinedFunc);

Builder.CreateBr(preLoopBB);
Builder.SetInsertPoint(preLoopBB);

Builder.CreateBr(condBB);

Builder.SetInsertPoint(condBB);
llvm::Value *loopVarValLoad = Builder.CreateLoad(loopVarVal->getType(), loopVar, node->variable);
llvm::Value *cond = Builder.CreateICmpSLT(loopVarValLoad, stopVal, "loopcond");

Builder.CreateCondBr(cond, bodyBB, endBB);

Builder.SetInsertPoint(bodyBB);

for (size_t i = 0; i < node->body_count; i++) {
generate_stmt(node->body[i], Context, Module, Builder);
}
Expand Down
8 changes: 7 additions & 1 deletion src/backend/generator/utils/generate_stop.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include "backend/generator/expressions/generate_expr.hpp"
#include "backend/generator/utils/generate_stop.hpp"
#include "backend/generator/utils/return_id.hpp"

llvm::Value *generate_stop(AstNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) {
BinaryExprNode *bin_expr = (BinaryExprNode *)node->data;
return generate_expr(bin_expr->right, Context, Builder, Module);

global_id_return = "declaration";
llvm::Value *expr = generate_expr(bin_expr->right, Context, Builder, Module);
global_id_return = "value";

return expr;
}
3 changes: 3 additions & 0 deletions src/backend/generator/utils/return_id.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "backend/generator/utils/return_id.hpp"

std::string global_id_return;
9 changes: 7 additions & 2 deletions src/frontend/parser/statements/parse_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ AstNode *parse_stmt(Parser *parser) {
}

// Attempt to parse a type. If this fails, it's likely an expression.
char *type = parse_type(parser);
char *type;

if (next_token(parser).type != TOKEN_ASSIGN) {
type = parse_type(parser);
} else {
return parse_expr(parser);
}

// If the type is "implicit", this means it's an ambiguous type.
if (strcmp(type, "implicit") == 0) {
Expand All @@ -64,7 +70,6 @@ AstNode *parse_stmt(Parser *parser) {
return parse_variable_declaration_stmt(parser, isPtr, isConst, type);
}

// If it's not a declaration, treat it as a generic expression.
return parse_expr(parser);
}
}
Expand Down

0 comments on commit 6eaa769

Please sign in to comment.