Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 145 additions & 4 deletions gen/MLIR/MLIRDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,83 @@ mlir::Value MLIRDeclaration::mlirGen(Declaration *declaration){
declaration->toChars());
LOG_SCOPE

if(auto varDeclaration = declaration->isVarDeclaration())
if(StructDeclaration *structDeclaration = declaration->isStructDeclaration()) {
if (failed(mlirGen(structDeclaration)))
return nullptr;
} else if(auto varDeclaration = declaration->isVarDeclaration())
return mlirGen(varDeclaration);
else {
IF_LOG Logger::println("Unable to recoganize Declaration: '%s'",
IF_LOG Logger::println(" "
"---------------------------------------------------- Unable to recoganize Declaration: '%s'",
declaration->toChars());
_miss++;
return nullptr;
}
}

mlir::LogicalResult MLIRDeclaration::mlirGen(StructDeclaration
*structDeclaration) {
IF_LOG Logger::println("MLIRCodeGen - StructDeclaration: '%s'",
structDeclaration->toChars());

if(structMap.count(structDeclaration->toChars()))
return mlir::emitError(loc(structDeclaration->loc)) << "error: struct "
"type with name '" << structDeclaration->toChars() << "' already exists";

auto variables = structDeclaration->members;
std::vector<mlir::Type> elementTypes;
elementTypes.reserve(variables->size());
for (auto variable : *variables){
Logger::println("Getting Type of '%s'", variable->toChars());
if(variable->hasStaticCtorOrDtor())
return mlir::emitError(loc(structDeclaration->loc)) << "error: "
"variables within a struct definition must not have initializers";

mlir::Type type = get_MLIRtype(nullptr,variable->getType());
if(!type)
return mlir::failure();
elementTypes.push_back(type);
}

structMap.try_emplace(structDeclaration->toChars(),
mlir::D::StructType::get(elementTypes), structDeclaration);
return mlir::success();
}

StructDeclaration* MLIRDeclaration::getStructFor(StructLiteralExp
*structLiteralExp){
IF_LOG Logger::println("MLIRCodeGen - GetStructFor: '%s'",
structLiteralExp->toChars());
llvm::StringRef structName;
if (auto *decl = structLiteralExp->sd){
auto varIt = symbolTable.lookup(decl->toChars());
if(!varIt)
return nullptr;
structName = decl->toChars();
} else {
if(!structLiteralExp->elements)
return nullptr;
for(auto var : *structLiteralExp->elements)
Logger::println("Expression: '%s'", var->toChars());

}
return nullptr;
}

mlir::Value MLIRDeclaration::mlirGen(StructLiteralExp* structLiteralExp) {
IF_LOG Logger::println("MLIRCodeGen - StructLiteralExp: '%s'",
structLiteralExp->toChars());

mlir::ArrayAttr dataAttr;
mlir::Type dataType;
std::tie(dataAttr, dataType) = getConstantAttr(nullptr, structLiteralExp);

// Build the MLIR op `toy.struct_constant`. This invokes the
// `StructConstantOp::build` method.
return builder.create<mlir::D::StructConstantOp>(loc(structLiteralExp->loc),
dataType, dataAttr);
}

mlir::Value MLIRDeclaration::mlirGen(VarDeclaration *vd){
IF_LOG Logger::println("MLIRCodeGen - VarDeclaration: '%s'", vd->toChars
());
Expand Down Expand Up @@ -81,6 +148,56 @@ mlir::Value MLIRDeclaration::mlirGen(VarDeclaration *vd){
return nullptr;
}

mlir::DenseElementsAttr MLIRDeclaration::getConstantAttr(mlir::Value value) {
// The type of this attribute is tensor of 64-bit floating-point with no
// shape.
auto dataType = mlir::RankedTensorType::get({}, value.getType());

// This is the actual attribute that holds the list of values for this
// tensor literal.
return mlir::DenseElementsAttr::get(dataType, &value);
}

std::pair<mlir::ArrayAttr, mlir::Type> MLIRDeclaration::getConstantAttr(
StructDeclaration *decl, StructLiteralExp *lit){

/// Emit a constant for a struct literal. It will be emitted as an array of
/// other literals in an Attribute attached to a `toy.struct_constant`
/// operation. This function returns the generated constant, along with the
/// corresponding struct type.
std::vector<mlir::Attribute> attrElements;
std::vector<mlir::Type> typeElements;

if(decl != nullptr && lit == nullptr){
IF_LOG Logger::println("MLIRCodeGen - Getting ConstantAttr for Struct: "
"'%s'", decl->toChars());

for(auto &var : *decl->members) {
Logger::println("Var: '%s'", var->toChars());
}
}else {
IF_LOG Logger::println("MLIRCodeGen - Getting ConstantAttr for Struct: "
"'%s'", lit->toChars());

for (auto &var : *lit->elements) {
if (auto *number = var->isIntegerExp()) {
auto value = mlirGen(number);
attrElements.push_back(getConstantAttr(value));
typeElements.push_back(value.getType());
} else if (auto *real = var->isRealExp()) {
auto value = mlirGen(real);
attrElements.push_back(getConstantAttr(value));
typeElements.push_back(value.getType());
}

}
}

mlir::ArrayAttr dataAttr = builder.getArrayAttr(attrElements);
mlir::Type dataType = mlir::D::StructType::get(typeElements);
return std::make_pair(dataAttr, dataType);
}

mlir::Value MLIRDeclaration::DtoAssignMLIR(mlir::Location Loc,
mlir::Value lhs, mlir::Value rhs, StringRef lhs_name,
StringRef rhs_name, int op, bool canSkipPostblit, Type* t1,
Expand Down Expand Up @@ -198,7 +315,7 @@ mlir::Value MLIRDeclaration::mlirGen(ArrayLiteralExp *arrayLiteralExp){
// shape .
mlir::Type elementType = get_MLIRtype(arrayLiteralExp->elements->data[0]);
bool isFloat = elementType.isF16() || elementType.isF32() || elementType.isF64();

Logger::println("@@@@@@@@@@@@@@@@@@@@@@@@ pegou o tipo!");

int size = 0;
if(elementType.isInteger(1))
Expand Down Expand Up @@ -477,6 +594,9 @@ mlir::Value MLIRDeclaration::mlirGen(DeclarationExp *decl_exp){

if (VarDeclaration *vd = dsym->isVarDeclaration())
return mlirGen(vd);
else if(StructDeclaration *structDeclaration = dsym->isStructDeclaration())
if(failed(mlirGen(structDeclaration)))
return nullptr;

IF_LOG Logger::println("Unable to recoganize DeclarationExp: '%s'",
dsym->toChars());
Expand Down Expand Up @@ -1034,9 +1154,30 @@ mlir::Value MLIRDeclaration::mlirGen(Expression *expression, mlir::Block* block)
return mlirGen(expression->isOrExp(),expression->isOrAssignExp());
else if (expression->isXorExp() || expression->isXorAssignExp())
return mlirGen(expression->isXorExp(),expression->isXorAssignExp());
else if(expression->isStructLiteralExp() || expression->isBlitExp()){
Logger::println(" ----> PANIC: STRUCT: '%s'", expression->toChars());
if(BlitExp* blit = expression->isBlitExp()){
Logger::println(" ----> PANIC: Blit: '%s'", blit->toChars());
Logger::println(" ----> PANIC: BLIT->E1: '%s'", blit->e1->toChars());
mlirGen(blit->e1);
Logger::println(" ----> PANIC: BLIT->E2: '%s'", blit->e2->toChars());
mlirGen(blit->e2);
}else {
Logger::println(" ----> PANIC: STRUCTLiteral: '%s'", expression->toChars
());
}
// auto cu = expression->isBlitExp();
//
//
// mlirGen(expression->isStructLiteralExp()->sd);
//getStructFor(expression->isStructLiteralExp());
//mlirGen(expression->isStructLiteralExp());
}


_miss++;
IF_LOG Logger::println("Unable to recoganize the Expression: '%s' : '%u': '%s'",
IF_LOG Logger::println(" --------------------------------------------------- "
"Unable to recoganize the Expression: '%s' : '%u': '%s'",
expression->toChars(), expression->op,
expression->type->toChars());
return nullptr;
Expand Down
7 changes: 7 additions & 0 deletions gen/MLIR/MLIRDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ class MLIRDeclaration {
unsigned &total, unsigned &miss);
~MLIRDeclaration();

mlir::LogicalResult mlirGen(StructDeclaration* structDeclaration);
mlir::Value mlirGen(VarDeclaration* varDeclaration);
mlir::Value mlirGen(Declaration* declaration);
mlir::DenseElementsAttr getConstantAttr(mlir::Value value);
StructDeclaration* getStructFor(StructLiteralExp *structLiteralExp);
std::pair<mlir::ArrayAttr, mlir::Type> getConstantAttr(StructDeclaration
*structDeclaration = nullptr, StructLiteralExp *structLiteralExp = nullptr);
mlir::Value DtoAssignMLIR(mlir::Location Loc, mlir::Value lhs,
mlir::Value rhs, StringRef lhs_name, StringRef rhs_name, int op,
bool canSkipPostblitm, Type* t1, Type* t2);
Expand All @@ -106,6 +111,8 @@ class MLIRDeclaration {
mlir::Value mlirGen(OrExp *orExp = nullptr, OrAssignExp *orAssignExp = nullptr);
mlir::Value mlirGen(PostExp *postExp);
mlir::Value mlirGen(RealExp *realExp);
mlir::Value mlirGen(StringExp *stringExp);
mlir::Value mlirGen(StructLiteralExp* structLiteralExp);
mlir::Value mlirGen(VarExp *varExp);
mlir::Value mlirGen(XorExp *xorExp = nullptr, XorAssignExp *xorAssignExp = nullptr);
void mlirGen(TemplateInstance *templateInstance);
Expand Down
16 changes: 16 additions & 0 deletions gen/MLIR/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ class MLIRGenImpl {
if (!func)
return nullptr;
theModule.push_back(func);
} else if (StructDeclaration *structDecl = dsym->isStructDeclaration()){
if(failed(declaration.mlirGen(structDecl)))
return nullptr;
IF_LOG Logger::println("MLIRCodeGen - StructLiteralExp: '%s'",
structDecl->toChars());
llvm::StringRef structName;
if (auto *decl = structDecl){
structName = decl->toChars();
} else {
if(!structDecl->members)
return nullptr;
for(auto var : *structDecl->members)
Logger::println("Expression: '%s'", var->toChars());

}

} else if (dsym->isInstantiated()) {
IF_LOG Logger::println("isTemplateInstance: '%s'",
dsym->isTemplateInstance()->toChars());
Expand Down