Skip to content

String support #20

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

Open
wants to merge 3 commits into
base: latestMain
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions mlir/examples/dsp/SimpleBlocks/include/toy/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ExprAST {
Expr_BinOp,
Expr_Call,
Expr_Print,
Expr_String,
};

ExprAST(ExprASTKind kind, Location location)
Expand Down Expand Up @@ -107,6 +108,20 @@ class VariableExprAST : public ExprAST {
static bool classof(const ExprAST *c) { return c->getKind() == Expr_Var; }
};

/// Expression class for string val.
class StringExprAST : public ExprAST {
std::string string_val;

public:
StringExprAST(Location loc, llvm::StringRef string_val)
: ExprAST(Expr_String, std::move(loc)), string_val(string_val) {}

llvm::StringRef getStringVal() { return string_val; }

/// LLVM style RTTI
static bool classof(const ExprAST *c) { return c->getKind() == Expr_String; }
};

/// Expression class for defining a variable.
class VarDeclExprAST : public ExprAST {
std::string name;
Expand Down
20 changes: 20 additions & 0 deletions mlir/examples/dsp/SimpleBlocks/include/toy/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum Token : int {
tok_return = -2,
tok_var = -3,
tok_def = -4,
tok_string_val = -7,

// primary
tok_identifier = -5,
Expand Down Expand Up @@ -84,6 +85,11 @@ class Lexer {
return identifierStr;
}

llvm::StringRef getString() {
assert(curTok == tok_string_val);
return stringVal;
}

/// Return the current number (prereq: getCurToken() == tok_number)
double getValue() {
assert(curTok == tok_number);
Expand Down Expand Up @@ -173,6 +179,17 @@ class Lexer {
return getTok();
}

// String val: "..."
if(lastChar == '"') {
stringVal = "";
while (isalnum((lastChar = Token(getNextChar()))) || lastChar == '_' || lastChar== ' ') {
if(lastChar == '"') break;
stringVal += (char)lastChar;
}
lastChar = Token(getNextChar());
return tok_string_val;
}

// Check for end of file. Don't eat the EOF.
if (lastChar == EOF)
return tok_eof;
Expand All @@ -191,6 +208,9 @@ class Lexer {

/// If the current Token is an identifier, this string contains the value.
std::string identifierStr;

// If current Token is a string val
std::string stringVal;

/// If the current Token is a number, this contains the value.
double numVal = 0;
Expand Down
21 changes: 19 additions & 2 deletions mlir/examples/dsp/SimpleBlocks/include/toy/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ class Parser {
return v;
}

/// parenexpr ::= '"' string_val '"'
std::unique_ptr<ExprAST> parseStringExpr() {
auto loc = lexer.getLastLocation();

std::string string_val(lexer.getString());
lexer.consume(tok_string_val);

return std::make_unique<StringExprAST>(std::move(loc), string_val);
}

/// identifierexpr
/// ::= identifier
/// ::= identifier '(' expression ')'
Expand All @@ -175,7 +185,7 @@ class Parser {

auto loc = lexer.getLastLocation();
lexer.getNextToken(); // eat identifier.

if (lexer.getCurToken() != '(') // Simple variable ref.
return std::make_unique<VariableExprAST>(std::move(loc), name);

Expand Down Expand Up @@ -216,6 +226,7 @@ class Parser {
/// ::= numberexpr
/// ::= parenexpr
/// ::= tensorliteral
/// ::= stringexpr
std::unique_ptr<ExprAST> parsePrimary() {
switch (lexer.getCurToken()) {
default:
Expand All @@ -230,6 +241,8 @@ class Parser {
return parseParenExpr();
case '[':
return parseTensorLiteralExpr();
case tok_string_val:
return parseStringExpr();
case ';':
return nullptr;
case '}':
Expand Down Expand Up @@ -334,7 +347,11 @@ class Parser {
if (!type)
type = std::make_unique<VarType>();
lexer.consume(Token('='));
auto expr = parseExpression();
std::unique_ptr<ExprAST> expr;
if(lexer.getCurToken() == tok_string_val) {
expr = parseStringExpr();
}
else expr = parseExpression();
return std::make_unique<VarDeclExprAST>(std::move(loc), std::move(id),
std::move(*type), std::move(expr));
}
Expand Down
25 changes: 25 additions & 0 deletions mlir/examples/dsp/SimpleBlocks/mlir/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <numeric>
#include <optional>
#include <vector>
#include <bitset>

using namespace mlir::dsp;
using namespace dsp;
Expand Down Expand Up @@ -947,6 +948,28 @@ class MLIRGenImpl {
mlir::Value mlirGen(NumberExprAST &num) {
return builder.create<ConstantOp>(loc(num.loc()), num.getValue());
}

/// Emit a string exression
mlir::Value mlirGen(StringExprAST &expr) {
auto string_val = expr.getStringVal();

std::vector<double> signals;
for(char ch : string_val) {
std::bitset<8> bits(static_cast<unsigned char>(ch)), reversed;
int n = 8;
for(int i=0; i<n; ++i) reversed[i] = bits[n-i-1];
for(int i=0; i<n; ++i) signals.push_back(reversed[i]);
}

mlir::Type eleType = builder.getF64Type();
auto dataType = mlir::RankedTensorType::get(signals.size(), eleType);

auto dataAttr = mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(signals));

auto type = getType(signals.size());

return builder.create<ConstantOp>(loc(expr.loc()), type, dataAttr);
}

/// Dispatch codegen for the right expression subclass using RTTI.
mlir::Value mlirGen(ExprAST &expr) {
Expand All @@ -961,6 +984,8 @@ class MLIRGenImpl {
return mlirGen(cast<CallExprAST>(expr));
case dsp::ExprAST::Expr_Num:
return mlirGen(cast<NumberExprAST>(expr));
case dsp::ExprAST::Expr_String:
return mlirGen(cast<StringExprAST>(expr));
default:
emitError(loc(expr.loc()))
<< "MLIR codegen encountered an unhandled expr kind '"
Expand Down
10 changes: 9 additions & 1 deletion mlir/examples/dsp/SimpleBlocks/parser/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ASTDumper {
void dump(PrintExprAST *node);
void dump(PrototypeAST *node);
void dump(FunctionAST *node);
void dump(StringExprAST *node);

// Actually print spaces matching the current indentation level
void indent() {
Expand Down Expand Up @@ -81,7 +82,7 @@ static std::string loc(T *node) {
void ASTDumper::dump(ExprAST *expr) {
llvm::TypeSwitch<ExprAST *>(expr)
.Case<BinaryExprAST, CallExprAST, LiteralExprAST, NumberExprAST,
PrintExprAST, ReturnExprAST, VarDeclExprAST, VariableExprAST>(
PrintExprAST, ReturnExprAST, VarDeclExprAST, StringExprAST, VariableExprAST>(
[&](auto *node) { this->dump(node); })
.Default([&](ExprAST *) {
// No match, fallback to a generic message
Expand All @@ -90,6 +91,13 @@ void ASTDumper::dump(ExprAST *expr) {
});
}

/// A string expression
void ASTDumper::dump(StringExprAST *stringExpr) {
INDENT();
llvm::errs() << "StringExpr \"" << stringExpr->getStringVal() << "\"";
llvm::errs() << " " << loc(stringExpr) << "\n";
}

/// A variable declaration is printing the variable name, the type, and then
/// recurse in the initializer value.
void ASTDumper::dump(VarDeclExprAST *varDecl) {
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Examples/DspExample/dsp_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def main() {
var a = [[[10,20],[30,0]] ];
var b = [[[40,50],[60,70]] ];
var c = sub(a, b);
print(c);

var d = "HELLO FROM SPACE";
print(d);
print("abd");
}