[TRITON][LANG] Added support for bitcast

This commit is contained in:
Philippe Tillet
2020-02-09 20:09:27 -05:00
committed by Philippe Tillet
parent 7a40077bfd
commit d22cf4f717
8 changed files with 42 additions and 3 deletions

View File

@@ -431,6 +431,7 @@ public:
void DerefOpTypeChecking(); void DerefOpTypeChecking();
void ReduceOpTypeChecking(); void ReduceOpTypeChecking();
void UnaryArithmOpTypeChecking(); void UnaryArithmOpTypeChecking();
void BitcastOpTypeChecking();
void CastOpTypeChecking(); void CastOpTypeChecking();
protected: protected:

View File

@@ -91,7 +91,8 @@ protected:
ir::value* GenAssignOp(Expr* lvalue, ir::value* rhs); ir::value* GenAssignOp(Expr* lvalue, ir::value* rhs);
ir::value* GenBroadcastOp(ir::value* src, ir::type* dst_ty); ir::value* GenBroadcastOp(ir::value* src, ir::type* dst_ty);
ir::value* GenNumcastOp(ir::value*src, ir::type* dst_ty); ir::value* GenNumcastOp(ir::value*src, ir::type* dst_ty);
ir::value* GenCastOp(ir::value* op, ir::type* type); ir::value* GenSemCastOp(ir::value* op, ir::type* type);
ir::value* GenBitCastOp(ir::value* src, ir::type* dst_ty);
// Triton-IR types // Triton-IR types
static ir::type* GenIRType(::Type* type, ir::context &ctx); static ir::type* GenIRType(::Type* type, ir::context &ctx);

View File

@@ -164,6 +164,7 @@ public:
ALIGNOF, // _Alignof ALIGNOF, // _Alignof
GENERIC, // _Generic GENERIC, // _Generic
IMAGINARY, // _Imaginary IMAGINARY, // _Imaginary
BITCAST,
// KEYWORD END // KEYWORD END
IDENTIFIER, IDENTIFIER,

View File

@@ -646,6 +646,9 @@ void UnaryOp::TypeChecking() {
case '!': case '!':
return UnaryArithmOpTypeChecking(); return UnaryArithmOpTypeChecking();
case Token::BITCAST:
return BitcastOpTypeChecking();
case Token::CAST: case Token::CAST:
return CastOpTypeChecking(); return CastOpTypeChecking();
@@ -722,6 +725,11 @@ void UnaryOp::UnaryArithmOpTypeChecking() {
} }
} }
void UnaryOp::BitcastOpTypeChecking() {
auto operandType = Type::MayCast(operand_->Type());
if(type_->Width() != operandType->Width())
Error(this, "cannot bitcast to type of different width");
}
void UnaryOp::CastOpTypeChecking() { void UnaryOp::CastOpTypeChecking() {
auto operandType = Type::MayCast(operand_->Type()); auto operandType = Type::MayCast(operand_->Type());

View File

@@ -197,7 +197,8 @@ void Generator::VisitUnaryOp(UnaryOp* unary) {
case Token::MINUS: return set_ret(GenUnaryMinus(arg)); case Token::MINUS: return set_ret(GenUnaryMinus(arg));
case '~': return error_not_implemented(); case '~': return error_not_implemented();
case '!': return error_not_implemented(); case '!': return error_not_implemented();
case Token::CAST: return set_ret(GenCastOp(arg, GenIRType(unary->Type(), *ctx_))); case Token::BITCAST: return set_ret(GenBitCastOp(arg, GenIRType(unary->Type(), *ctx_)));
case Token::CAST: return set_ret(GenSemCastOp(arg, GenIRType(unary->Type(), *ctx_)));
case Token::REDUCE: { case Token::REDUCE: {
int ax, tag; int ax, tag;
UnaryOp::decodeRed(unary->info_, ax, tag); UnaryOp::decodeRed(unary->info_, ax, tag);
@@ -579,10 +580,15 @@ ir::value* Generator::GenNumcastOp(ir::value*src, ir::type* dst_ty) {
} }
} }
ir::value* Generator::GenCastOp(ir::value* src, ir::type* dst_ty) { ir::value* Generator::GenSemCastOp(ir::value* src, ir::type* dst_ty) {
return GenNumcastOp(GenBroadcastOp(src, dst_ty), dst_ty); return GenNumcastOp(GenBroadcastOp(src, dst_ty), dst_ty);
} }
ir::value* Generator::GenBitCastOp(ir::value* src, ir::type* dst_ty) {
return bld_->create_cast(ir::BitCast, GenBroadcastOp(src, dst_ty), dst_ty);
}
// Triton-IR Attr // Triton-IR Attr
ir::attribute Generator::GenIRAttr(ASTNode::Attr attr) { ir::attribute Generator::GenIRAttr(ASTNode::Attr attr) {
if(attr.kind == ASTNode::Attr::MULTIPLEOF) { if(attr.kind == ASTNode::Attr::MULTIPLEOF) {

View File

@@ -664,6 +664,17 @@ QualType Parser::ParseTypeName() {
Expr* Parser::ParseCastExpr() { Expr* Parser::ParseCastExpr() {
auto tok = ts_.Next(); auto tok = ts_.Next();
// bitcast
if (tok->tag_ == Token::BITCAST) {
ts_.Expect('<');
auto type = ParseTypeName();
ts_.Expect('>');
ts_.Expect('(');
auto operand = ParseExpr();
ts_.Expect(')');
return UnaryOp::New(Token::BITCAST, operand, type);
}
// semantic cast
if (tok->tag_ == '(' && IsTypeName(ts_.Peek())) { if (tok->tag_ == '(' && IsTypeName(ts_.Peek())) {
auto type = ParseTypeName(); auto type = ParseTypeName();
ts_.Expect(')'); ts_.Expect(')');

View File

@@ -44,6 +44,7 @@ const std::unordered_map<std::string, int> Token::kwTypeMap_ {
{ "void", Token::VOID }, { "void", Token::VOID },
{ "volatile", Token::VOLATILE }, { "volatile", Token::VOLATILE },
{ "while", Token::WHILE }, { "while", Token::WHILE },
{ "bitcast", Token::BITCAST },
{ "_Alignas", Token::ALIGNAS }, { "_Alignas", Token::ALIGNAS },
{ "_Alignof", Token::ALIGNOF }, { "_Alignof", Token::ALIGNOF },
{ "_Atomic", Token::ATOMIC }, { "_Atomic", Token::ATOMIC },
@@ -145,6 +146,7 @@ const std::unordered_map<int, const char*> Token::tagLexemeMap_ {
{ Token::VOID, "void" }, { Token::VOID, "void" },
{ Token::VOLATILE, "volatile" }, { Token::VOLATILE, "volatile" },
{ Token::WHILE, "while" }, { Token::WHILE, "while" },
{ Token::BITCAST, "bitcast" },
{ Token::ALIGNAS, "_Alignas" }, { Token::ALIGNAS, "_Alignas" },
{ Token::ALIGNOF, "_Alignof" }, { Token::ALIGNOF, "_Alignof" },
{ Token::ATOMIC, "_Atomic" }, { Token::ATOMIC, "_Atomic" },

View File

@@ -283,6 +283,15 @@ extern int get_num_programs(int);
extern float sqrtf(float); extern float sqrtf(float);
extern int select(bool, int, int); extern int select(bool, int, int);
extern char __constant__ * calloc(int); extern char __constant__ * calloc(int);
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long uint64;
typedef char int8;
typedef short int16;
typedef int int32;
typedef long int64;
)"; )";
} }