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

[frontend] Add frontend generator. #352

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
64a48d1
[FrontendGen] Add Frontend Generator.
CBalaa Jul 8, 2024
93f69eb
[FrontendGen] Restructure class Expression.
CBalaa Jul 8, 2024
ed4fd9d
update fegen
FloatingcloudKnight Jul 11, 2024
4a6cf8c
[FrontendGen] Fix type error in visitor.
CBalaa Jul 15, 2024
2c78e0a
[FrontendGen] Rename: remove 'Fegen' prefix.
CBalaa Jul 15, 2024
2643f85
Merge pull request #8 from CBalaa/fegen-chh
CBalaa Jul 15, 2024
0968b60
[FrontendGen] add function codegen
FloatingcloudKnight Jul 15, 2024
56a6415
[FrontendGen] Merge branch 'main' of https://github.com/CBalaa/buddy-…
FloatingcloudKnight Jul 15, 2024
0e0a16d
[FrontendGen] Merge branch 'main' of https://github.com/CBalaa/buddy-…
FloatingcloudKnight Jul 15, 2024
5a86147
[FrontendGen] update function codegen
FloatingcloudKnight Jul 17, 2024
d390aad
[FrontendGen] Refactor fegen Type.
CBalaa Jul 17, 2024
8ba4f6f
Merge branch 'fegen-chh' into main-fegen
CBalaa Jul 18, 2024
da506b7
[FrontendGen] update function codegen
FloatingcloudKnight Jul 18, 2024
c6dfcf7
[FrontendGen] Refactor fegen::Type, add TypeTemplate subclasses, whic…
CBalaa Jul 18, 2024
dd07970
[FrontendGen] Refactor fegen Type, add Template classes.
CBalaa Jul 19, 2024
3ee16db
Merge branch 'fegen-cww' into main-fegen
CBalaa Jul 19, 2024
446a1cf
[FrontendGen] Add function generation.
CBalaa Jul 19, 2024
2ea7663
[FrontendGen] Fix FloatPointType getter.
CBalaa Jul 22, 2024
4d6dfed
[FrontendGen] update function generation.
FloatingcloudKnight Jul 23, 2024
87bc2fa
[FrontendGen] Add function generation.
FloatingcloudKnight Jul 23, 2024
cde5c7e
[FrontendGen] update function generation.
FloatingcloudKnight Jul 23, 2024
1060178
[Frontend] Update function generation.
CBalaa Jul 23, 2024
046aa61
Merge branch 'main-fegen' into fegen-chh
CBalaa Jul 25, 2024
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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ check_toolchain()
# NB: currently, ANTLR is used in dsl examples only,
# however, there is a plan to use in the frontend,
# so it is kept in the top-level cmake
if(BUDDY_DSL_EXAMPLES)
if(BUDDY_DSL_EXAMPLES OR FeGen)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Antlr)

# required if linking to static library
Expand Down
7 changes: 4 additions & 3 deletions examples/FrontendGen/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Toy.g4
MLIRToyVisitor.h

test/
*.g4
*.td
*.cpp
247 changes: 106 additions & 141 deletions examples/FrontendGen/example.fegen
Original file line number Diff line number Diff line change
@@ -1,141 +1,106 @@
dialect Toy_Dialect
: name = "toy"
: cppNamespace = "mlir::toy"
;

op ConstantOp
: arguments = (ins F64ElementsAttr : $value)
: results = (outs F64Tensor)
: builders = [
OpBuilder<(ins "DenseElementsAttr" : $value),
[{ build($_builder, $_state, value.getType(), value); }]>,
OpBuilder<(ins "double":$value)>]
;

op AddOp
: arguments = (ins F64Tensor : $lhs, F64Tensor: $rhs)
: results = (outs F64Tensor)
: builders = [OpBuilder<(ins "Value" : $lhs, "Value" : $rhs)>]
;

op CastOp
: arguments = (ins F64Tensor:$input)
: results = (outs F64Tensor:$output)
;

op FuncOp
: arguments = (ins
SymbolNameAttr:$sym_name,
TypeAttrOf<FunctionType>:$function_type
)
: builders = [ OpBuilder<(ins
"StringRef":$name, "FunctionType":$type,
CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs)>
]
;

op MulOp
: arguments = (ins F64Tensor:$lhs, F64Tensor:$rhs)
: results = (outs F64Tensor)
: builders = [
OpBuilder<(ins "Value":$lhs, "Value":$rhs)>
]
;

op PrintOp
: arguments = (ins AnyTypeOf<[F64Tensor, F64MemRef]>:$input)
;

op ReshapeOp
: arguments = (ins F64Tensor : $input)
: results = (outs StaticShapeTensorOf<[F64]>)
;

op ReturnOp
: arguments = (ins Variadic<F64Tensor>:$input)
: builders = [
OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]>
]
;

op GenericCallOp
: arguments = (ins FlatSymbolRefAttr:$callee, Variadic<F64Tensor>:$inputs)
: results = (outs F64Tensor)
: builders = [
OpBuilder<(ins "StringRef":$callee, "ArrayRef<Value>":$arguments)>
]
;

op TransposeOp
: arguments = (ins F64Tensor:$input)
: results = (outs F64Tensor)
: builders = [
OpBuilder<(ins "Value":$input)>
]
;


rule module
: funDefine
;

rule expression
: Number
: tensorLiteral
: identifierExpr
: expression Add expression
;

rule returnExpr
: Return expression?
;

rule identifierExpr
: Identifier
: Identifier ParentheseOpen (expression (Comma expression) *)? ParentheseClose {
builder = GenericCallOp_1, PrintOp_0
}
;

rule tensorLiteral
: SbracketOpen ( tensorLiteral ( Comma tensorLiteral ) *) ? SbracketClose
: Number
;

rule varDecl
: Var Identifier (type) ? (Equal expression) ? {
builder = ReshapeOp_0
}
;

rule type
: AngleBracketOpen Number(Comma Number) * AngleBracketClose
;

rule funDefine
: prototype block {
builder = ReturnOp_1
}
;

rule prototype
: Def Identifier ParentheseOpen declList ? ParentheseClose {
builder = FuncOp_0
}
;

rule declList
: Identifier
: Identifier Comma declList
;

rule block
: BracketOpen(blockExpr Semi) * BracketClose
;

rule blockExpr
: varDecl
: returnExpr
: expression
;

fegen toy

typedef struct {
parameters [list<Type> elementTypes] // ArrayParameter<'Type'>
}

Type Toy_Type = any<[Tensor, struct]>;

opdef constant {
arguments [operand list<Integer<size>> numberAttr] // Variadic<Builtin_Integer>
results [operand Tensor res]
body {
list<int> shape = shapeOf(res);
// full是一个内置函数,创建memref,并将每个元素都填充numberAttr
res = full(shape, numberAttr);
}
}

opdef add {
arguments [operand Tensor lhs, operand Tensor rhs]
results [operand Tensor res]
body {
// 这个'+'也是一个内置的函数
res = lhs + rhs; // res = builder.create<add>(lsh, rhs);
}
}

opdef mul {
arguments [operand Tensor lhs, operand Tensor rhs]
results [operand Tensor res]
body {
// 这个'*'也是一个内置的函数
res = lhs * rhs;
}
}

opdef reshape {
arguments [operand F64Tensor input]
results [operand F64Tensor output]
body {
list<int> shape = shapeOf(output);
output = reshape(input, shape);
}
}

double stod(string numStr){
double res = 0;
int index;
int i;
for(i = 0; i <= len(numStr)-1; i=i+1){
char c = numStr[0];
int charNum;
if(c == '0'){
charNum = 0;
}else if (c == '1'){
charNum = 1;
}else if (c == '2'){
charNum = 2;
}else if (c == '3'){
charNum = 3;
}else if (c == '4'){
charNum = 4;
}else if (c == '5'){
charNum = 5;
}else if (c == '6'){
charNum = 6;
}else if (c == '7'){
charNum = 7;
}else if (c == '8'){
charNum = 8;
}else if (c == '9'){
charNum = 9;
}else if (c == '.'){
index = i;
}
res = res * 10;
res = res + charNum;
}
res = res * 0.1**(len(numStr) - 1 - index);
return res;
}


module
: structDefine* funDefine+
;

structDefine
: Struct Identifier BracketOpen (varDecl Semicolon)* BracketClose
;

// cpp value --get--> mlir::attribute || --constant Operation--> mlir::Value
// ======== ||

expression
: Number
{
returns [operand F64Tensor ret, operand F64Tensor ret]
actions {
// Type mlir::Value ret of operator | Attribute | Cpp Value
double numberAttr = stod($Number().getText());
Type retType = Tensor<[], double>;
ret = constant(numberAttr, retType);
}
}
;
18 changes: 18 additions & 0 deletions examples/FrontendGen/function.fegen
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fegen toy

double stod(string numStr){
float res = 0.0;
int c = 1;
for(int i = 0; i < 3; i = i+1){
if(c == 0){
int charNum = 0;
int intNum = 1;
intNum = 1;
}else if (c == 1){
int charNum = 1;
}else {
int charNum = 2;
}
}
return res;
}
17 changes: 11 additions & 6 deletions examples/FrontendGen/makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/bin/bash
BUDDY_FRONTEND_GEN := ../../build/bin/buddy-frontendgen

frontendgen-emit-ast:
@${BUDDY_FRONTEND_GEN} -f ./example.fegen -emit=ast
opDefine:
@${BUDDY_FRONTEND_GEN} -f ./opDefine.fegen

frontendgen-emit-antlr:
@${BUDDY_FRONTEND_GEN} -f ./example.fegen -emit=antlr -g Toy
typeDefine:
@${BUDDY_FRONTEND_GEN} -f ./typeDefine.fegen

frontendgen-emit-visitor:
@${BUDDY_FRONTEND_GEN} -f ./example.fegen -emit=visitor -g Toy
rule:
@${BUDDY_FRONTEND_GEN} -f ./rule.fegen

function:
@${BUDDY_FRONTEND_GEN} -f ./function.fegen

clean:
rm -f ./toy*
14 changes: 14 additions & 0 deletions examples/FrontendGen/opDefine.fegen
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fegen toy

opdef add {
arguments [operand Integer lhs, operand Integer rhs]
results [operand Integer res]
body {
res = lhs + rhs;
}
}

opdef constant {
arguments [attribute double value]
results [operand Tensor<FloatPoint<64>> res]
}
14 changes: 14 additions & 0 deletions examples/FrontendGen/rule.fegen
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fegen toy

module
: structDefine* funDefine+
;

structDefine
: Struct Identifier BracketOpen (varDecl Semicolon)* BracketClose
;

expression
: Number
| Identifier
;
17 changes: 17 additions & 0 deletions examples/FrontendGen/typeDefine.fegen
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fegen toy

typedef struct {
parameters [list<Type> elementTypes]
}

typedef test1 {
parameters [Type e]
}

typedef test2 {
parameters [list<int> e]
}

typedef test3 {
parameters [int e]
}
4 changes: 3 additions & 1 deletion frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
add_subdirectory(FrontendGen)
if(FeGen)
add_subdirectory(FrontendGen)
endif()
add_subdirectory(Interfaces)
if(BUDDY_MLIR_ENABLE_PYTHON_PACKAGES)
add_subdirectory(Python)
Expand Down
1 change: 1 addition & 0 deletions frontend/FrontendGen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.antlr/
21 changes: 15 additions & 6 deletions frontend/FrontendGen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
link_directories("${CMAKE_CURRENT_BINARY_DIR}/lib")
add_subdirectory(lib)
set (LLVM_LINK_COMPONENTS
support
frontendgenlib
)
support
)

include_directories(${ANTLR_FegenLexer_OUTPUT_DIR})
include_directories(${ANTLR_FegenParser_OUTPUT_DIR})
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")

add_llvm_tool(buddy-frontendgen
frontendgen.cpp
frontendgen.cpp
)

target_link_libraries(buddy-frontendgen
PRIVATE
fegen_antlr_generated
fegenVisitor
antlr4_static
)
Loading