Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward Conversion #27

Merged
merged 52 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
3aedb90
setup for backwards conversion
Berke-Ates Apr 10, 2023
bf170f3
added partial state machine lowering
Berke-Ates Apr 12, 2023
111fdcb
added symbolic parser
Berke-Ates Apr 15, 2023
4aa2e1d
added backtracking to parser
Berke-Ates Apr 15, 2023
2a1ffcf
fixed binop parser bug
Berke-Ates Apr 15, 2023
9980728
started LR(1) parser
Berke-Ates Apr 16, 2023
d8a631b
implemented symbolic parser
Berke-Ates Apr 24, 2023
705cec2
replaced bare pointers by smart pointers
Berke-Ates Apr 24, 2023
9ad2626
added some codegen for binop & unop
Berke-Ates Apr 24, 2023
e7fefd6
implemented binop & unop codegens
Berke-Ates Apr 24, 2023
8a01e9b
bugfix
Berke-Ates Apr 24, 2023
918e731
added todos + edge reordering
Berke-Ates Apr 24, 2023
4c4219a
added framework for all conversions
Berke-Ates Apr 26, 2023
48f12a8
fixed return insertion
Berke-Ates May 2, 2023
b4685d0
added reference mapping
Berke-Ates May 2, 2023
92aed01
started type converter
Berke-Ates May 2, 2023
f7f0e52
added partial support for external calls
Berke-Ates May 26, 2023
c5e8994
added global code and side effects
Berke-Ates May 26, 2023
83781fe
extended translation capabilities
Berke-Ates May 29, 2023
8734d2c
added copy conversion + bugfixes
Berke-Ates May 30, 2023
4cca61b
changed zero-dimensional memref conversion
Berke-Ates May 31, 2023
4c1e6df
comments
Berke-Ates Jun 21, 2023
fe8dc9e
tests restructure
Berke-Ates Jun 27, 2023
acc9c28
added backward tests
Berke-Ates Jun 27, 2023
bf2b909
added tasklet lowering
Berke-Ates Jun 27, 2023
ca4e79c
added alloc, load, store and tasklet
Berke-Ates Jun 27, 2023
d5799e6
fixed alive value bug
Berke-Ates Jun 28, 2023
1e8cbcd
added copy & sym ops
Berke-Ates Jun 28, 2023
9afa6cf
added static keyword
Berke-Ates Jun 29, 2023
90513cd
addde map conversion
Berke-Ates Jun 29, 2023
3de7501
added dynamic sizes + symbolic indices
Berke-Ates Jul 1, 2023
fd7f09a
added symbolic array sizes
Berke-Ates Jul 2, 2023
532fe6c
added nested sdfgs + bugfix
Berke-Ates Jul 8, 2023
a57b461
added symbol propagation
Berke-Ates Jul 8, 2023
87db973
added symbols to top-level sdfg
Berke-Ates Jul 13, 2023
81e092d
added copyright headers
Berke-Ates Jul 23, 2023
06c2e3e
added further copyright headers
Berke-Ates Jul 23, 2023
98ca597
added comments
Berke-Ates Aug 2, 2023
09103eb
added dialect comments
Berke-Ates Aug 3, 2023
27c68c6
added further function comments
Berke-Ates Aug 4, 2023
a2c63de
added translation comments
Berke-Ates Aug 4, 2023
ff27fa0
added utils comments
Berke-Ates Aug 5, 2023
783a487
added conversion comments
Berke-Ates Aug 6, 2023
12c8e88
added dialect comments
Berke-Ates Aug 6, 2023
6fc0d43
added operation comments
Berke-Ates Aug 7, 2023
a60517c
added further comments on operations
Berke-Ates Aug 7, 2023
202dca9
Added further operation comments
Berke-Ates Aug 7, 2023
21ba752
added translation comments
Berke-Ates Aug 8, 2023
4e84201
added utility comments
Berke-Ates Aug 9, 2023
4a76932
added header comments
Berke-Ates Aug 10, 2023
c7f87b7
fixed translation bug
Berke-Ates Aug 10, 2023
7ec5e43
added further file comments and restructured tests
Berke-Ates Aug 10, 2023
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
2 changes: 2 additions & 0 deletions include/SDFG/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
add_subdirectory(GenericToSDFG)
add_subdirectory(LinalgToSDFG)

add_subdirectory(SDFGToGeneric)
2 changes: 1 addition & 1 deletion include/SDFG/Conversion/GenericToSDFG/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include "mlir/Pass/PassBase.td"
include "SDFG/Dialect/Dialect.td"

def GenericToSDFGPass : Pass<"convert-to-sdfg", "ModuleOp"> {
let summary = "Convert SCF, Arith and Memref dialect to SDFG dialect";
let summary = "Convert SCF, Arith, Math and Memref dialect to SDFG dialect";
let constructor = "mlir::sdfg::conversion::createGenericToSDFGPass()";
let dependentDialects = ["mlir::sdfg::SDFGDialect"];
let options = [
Expand Down
6 changes: 6 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name SDFGToGeneric)
add_public_tablegen_target(MLIRSDFGToGenericPassIncGen)

target_sources(SOURCE_FILES_H PRIVATE PassDetail.h Passes.h SymbolicParser.h
OpCreators.h)
104 changes: 104 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/OpCreators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef SDFG_Conversion_SDFGToGeneric_Op_Creators_H
#define SDFG_Conversion_SDFGToGeneric_Op_Creators_H

#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Transforms/DialectConversion.h"

using namespace mlir;

namespace mlir::sdfg::conversion {

func::FuncOp createFunc(PatternRewriter &rewriter, Location loc, StringRef name,
TypeRange inputTypes, TypeRange resultTypes,
StringRef visibility);

func::CallOp createCall(PatternRewriter &rewriter, Location loc,
TypeRange resultTypes, StringRef callee,
ValueRange operands);

func::ReturnOp createReturn(PatternRewriter &rewriter, Location loc,
ValueRange operands);

cf::BranchOp createBranch(PatternRewriter &rewriter, Location loc,
ValueRange operands, Block *dest);

cf::CondBranchOp createCondBranch(PatternRewriter &rewriter, Location loc,
Value condition, Block *trueDest,
Block *falseDest);

memref::AllocOp createAlloc(PatternRewriter &rewriter, Location loc,
MemRefType memrefType, ValueRange dynamicSizes);

memref::LoadOp createLoad(PatternRewriter &rewriter, Location loc, Value memref,
ValueRange indices);

memref::StoreOp createStore(PatternRewriter &rewriter, Location loc,
Value value, Value memref, ValueRange indices);

memref::CopyOp createCopy(PatternRewriter &rewriter, Location loc, Value source,
Value target);

void allocSymbol(PatternRewriter &rewriter, Location loc, StringRef symName,
llvm::StringMap<Value> &symbolMap);

arith::ConstantIntOp createConstantInt(PatternRewriter &rewriter, Location loc,
int val, int width);

arith::AddIOp createAddI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::SubIOp createSubI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::MulIOp createMulI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::DivSIOp createDivSI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::FloorDivSIOp createFloorDivSI(PatternRewriter &rewriter, Location loc,
Value a, Value b);

arith::RemSIOp createRemSI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::OrIOp createOrI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::AndIOp createAndI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::XOrIOp createXOrI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::ShLIOp createShLI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::ShRSIOp createShRSI(PatternRewriter &rewriter, Location loc, Value a,
Value b);

arith::CmpIOp createCmpI(PatternRewriter &rewriter, Location loc,
arith::CmpIPredicate predicate, Value lhs, Value rhs);

arith::ExtSIOp createExtSI(PatternRewriter &rewriter, Location loc, Type out,
Value in);

arith::TruncIOp createTruncI(PatternRewriter &rewriter, Location loc, Type out,
Value in);

arith::IndexCastOp createIndexCast(PatternRewriter &rewriter, Location loc,
Type out, Value in);

scf::ParallelOp createParallel(PatternRewriter &rewriter, Location loc,
ValueRange lowerBounds, ValueRange upperBounds,
ValueRange steps);

scf::YieldOp createYield(PatternRewriter &rewriter, Location loc);

} // namespace mlir::sdfg::conversion

#endif // SDFG_Conversion_SDFGToGeneric_Op_Creators_H
18 changes: 18 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/PassDetail.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SDFG_Conversion_SDFGToGeneric_PassDetail_H
#define SDFG_Conversion_SDFGToGeneric_PassDetail_H

#include "SDFG/Dialect/Dialect.h"
#include "mlir/Pass/Pass.h"

namespace mlir {
namespace sdfg {
namespace conversion {

#define GEN_PASS_CLASSES
#include "SDFG/Conversion/SDFGToGeneric/Passes.h.inc"

} // namespace conversion
} // namespace sdfg
} // end namespace mlir

#endif // SDFG_Conversion_SDFGToGeneric_PassDetail_H
21 changes: 21 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef SDFG_Conversion_SDFGToGeneric_H
#define SDFG_Conversion_SDFGToGeneric_H

#include "mlir/Pass/Pass.h"

namespace mlir::sdfg::conversion {

/// Creates a sdfg to generic converting pass
std::unique_ptr<Pass> createSDFGToGenericPass();

//===----------------------------------------------------------------------===//
// Registration
//===----------------------------------------------------------------------===//

/// Generate the code for registering passes.
#define GEN_PASS_REGISTRATION
#include "SDFG/Conversion/SDFGToGeneric/Passes.h.inc"

} // namespace mlir::sdfg::conversion

#endif // SDFG_Conversion_SDFGToGeneric_H
18 changes: 18 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/Passes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SDFG_Conversion_SDFGToGeneric
#define SDFG_Conversion_SDFGToGeneric

include "mlir/Pass/PassBase.td"
include "SDFG/Dialect/Dialect.td"

def SDFGToGenericPass : Pass<"lower-sdfg", "ModuleOp"> {
let summary = "Convert SDFG dialect to Func, CF, Memref and SCF dialects";
let constructor = "mlir::sdfg::conversion::createSDFGToGenericPass()";
let dependentDialects = [
"mlir::func::FuncDialect",
"mlir::cf::ControlFlowDialect",
"mlir::memref::MemRefDialect",
"mlir::scf::SCFDialect"
];
}

#endif // SDFG_Conversion_SDFGToGeneric
189 changes: 189 additions & 0 deletions include/SDFG/Conversion/SDFGToGeneric/SymbolicParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#ifndef SDFG_Conversion_SDFGToGeneric_Symbolic_Parser_H
#define SDFG_Conversion_SDFGToGeneric_Symbolic_Parser_H

#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Transforms/DialectConversion.h"

namespace mlir::sdfg::conversion {

class ASTNode {
public:
virtual ~ASTNode() = default;
virtual Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) = 0;
};

class IntNode : public ASTNode {
public:
int value;

IntNode(int value) : value(value){};

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

class BoolNode : public ASTNode {
public:
bool value;

BoolNode(bool value) : value(value) {}

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

class VarNode : public ASTNode {
public:
std::string name;

VarNode(std::string name) : name(name) {}

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

class AssignNode : public ASTNode {
public:
std::unique_ptr<VarNode> variable;
std::unique_ptr<ASTNode> expr;

AssignNode(std::unique_ptr<VarNode> variable, std::unique_ptr<ASTNode> expr)
: variable(std::move(variable)), expr(std::move(expr)) {}

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

class UnOpNode : public ASTNode {
public:
enum UnOp { ADD, SUB, LOG_NOT, BIT_NOT };
UnOp op;
std::unique_ptr<ASTNode> expr;

UnOpNode(UnOp op, std::unique_ptr<ASTNode> expr)
: op(op), expr(std::move(expr)) {}

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

class BinOpNode : public ASTNode {
public:
enum BinOp {
ADD,
SUB,
MUL,
DIV,
FLOORDIV,
MOD,
EXP,
BIT_OR,
BIT_XOR,
BIT_AND,
LSHIFT,
RSHIFT,
LOG_OR,
LOG_AND,
EQ,
NE,
LT,
LE,
GT,
GE
};

std::unique_ptr<ASTNode> left;
BinOp op;
std::unique_ptr<ASTNode> right;

BinOpNode(std::unique_ptr<ASTNode> left, BinOp op,
std::unique_ptr<ASTNode> right)
: left(std::move(left)), op(op), right(std::move(right)) {}

Value codegen(PatternRewriter &rewriter, Location loc,
llvm::StringMap<Value> &symbolMap,
llvm::StringMap<Value> &refMap) override;
};

enum TokenType {
EQ,
NE,
LT,
LE,
GT,
GE,
ASSIGN,
LOG_OR,
LOG_AND,
LOG_NOT,
ADD,
SUB,
MUL,
DIV,
FLOORDIV,
MOD,
EXP,
TRUE,
FALSE,
BIT_OR,
BIT_XOR,
BIT_AND,
BIT_NOT,
LSHIFT,
RSHIFT,
LPAREN,
RPAREN,
INT_CONST,
IDENT,
WS
};

struct Token {
TokenType type;
std::string value;
};

class SymbolicParser {
public:
std::unique_ptr<ASTNode> parse(StringRef input);

private:
unsigned pos;
SmallVector<Token> tokens;

Optional<SmallVector<Token>> tokenize(StringRef input);

std::unique_ptr<ASTNode> stmt();
std::unique_ptr<ASTNode> assignment();

std::unique_ptr<ASTNode> log_or_expr();
std::unique_ptr<ASTNode> log_and_expr();

std::unique_ptr<ASTNode> eq_expr();
std::unique_ptr<ASTNode> rel_expr();
std::unique_ptr<ASTNode> shift_expr();

std::unique_ptr<ASTNode> bit_or_expr();
std::unique_ptr<ASTNode> bit_xor_expr();
std::unique_ptr<ASTNode> bit_and_expr();

std::unique_ptr<ASTNode> add_expr();
std::unique_ptr<ASTNode> mul_expr();
std::unique_ptr<ASTNode> exp_expr();
std::unique_ptr<ASTNode> unary_expr();
std::unique_ptr<ASTNode> factor();

std::unique_ptr<ASTNode> const_expr();
std::unique_ptr<ASTNode> bool_const();
};

} // namespace mlir::sdfg::conversion

#endif // SDFG_Conversion_SDFGToGeneric_Symbolic_Parser_H
9 changes: 8 additions & 1 deletion include/SDFG/Dialect/Dialect.td
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def SDFG_SizedType : SDFG_Type<"Sized">{

size_t getUndefRank(){
size_t undefSize = 0;
for(int64_t dim : getIntegers()) if(dim == -1) undefSize++;
for(int64_t dim : getIntegers()) if(dim < 0) undefSize++;
return undefSize;
}

Expand All @@ -68,6 +68,13 @@ def SDFG_ArrayType : SDFG_Type<"Array">{

let parameters = (ins SDFG_SizedType:$dimensions);
let hasCustomAssemblyFormat = 1;

let extraClassDeclaration = [{
Type getElementType();
ArrayRef<StringAttr> getSymbols();
ArrayRef<int64_t> getIntegers();
ArrayRef<bool> getShape();
}];
}

//===----------------------------------------------------------------------===//
Expand Down
Loading