Skip to content

Commit aa2aa6f

Browse files
progress of fixes (#153)
* fixed issue with union type and compare * progress of fixing cast from union to union * fixing union type with char type * more fixes * finihsing cast for union types into union type * added fake cast to support safecast * finished save op for safecast * fixed issue with char typeof * update * fixed issue with debug info for async/await * temp hack for union and tuples * some fixes * more fixes to typeof * added ability to debug included code * fixed more issue with debug info and import/include * update build scripts
1 parent f38dcf7 commit aa2aa6f

File tree

17 files changed

+373
-65
lines changed

17 files changed

+373
-65
lines changed

tag.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
git tag -a v0.0-pre-alpha64 -m "pre alpha v0.0-64"
1+
git tag -a v0.0-pre-alpha65 -m "pre alpha v0.0-65"
22
git push origin --tags

tag_del.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
git push --delete origin v0.0-pre-alpha64
2-
git tag -d v0.0-pre-alpha64
1+
git push --delete origin v0.0-pre-alpha65
2+
git tag -d v0.0-pre-alpha65

tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ class CastLogicHelper
517517
if (auto inUnionType = dyn_cast<mlir_ts::UnionType>(inType))
518518
{
519519
// nothing to do
520+
LLVM_DEBUG(llvm::dbgs() << "\n\t cast union type to union type: " << inType << "->" << resType << "\n";);
520521
}
521522
else
522523
{
@@ -546,7 +547,22 @@ class CastLogicHelper
546547
return cast(in, baseType, tch.convertType(baseType), resType, resLLVMType);
547548
}
548549

549-
// skip to next steps
550+
if (auto resUnionType = dyn_cast<mlir_ts::UnionType>(resType))
551+
{
552+
mlir::Type baseTypeRes;
553+
bool needTagRes = mth.isUnionTypeNeedsTag(loc, resUnionType, baseTypeRes);
554+
if (needTagRes)
555+
{
556+
LLVMTypeConverterHelper ltch((const LLVMTypeConverter *)tch.typeConverter);
557+
auto maxStoreType = ltch.findMaxSizeType(inUnionType);
558+
auto value = rewriter.create<mlir_ts::GetValueFromUnionOp>(loc, maxStoreType, in);
559+
auto typeOfValue = rewriter.create<mlir_ts::GetTypeInfoFromUnionOp>(loc, mlir_ts::StringType::get(rewriter.getContext()), in);
560+
auto unionValue = rewriter.create<mlir_ts::CreateUnionInstanceOp>(loc, resType, value, typeOfValue);
561+
return unionValue;
562+
}
563+
}
564+
565+
// fall into default case
550566
}
551567

552568
if (auto undefType = dyn_cast<mlir_ts::UndefinedType>(inType))

tsc/include/TypeScript/LowerToLLVM/UnaryBinLogicalOrHelper.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ LogicalResult BinOp(BinOpTy &binOp, mlir::Value left, mlir::Value right, Pattern
8080
return success();
8181
}
8282

83+
template <typename T>
84+
std::string to_print(mlir::Type type)
85+
{
86+
SmallString<128> exportType;
87+
raw_svector_ostream rso(exportType);
88+
89+
MLIRPrinter mp{};
90+
mp.printType<raw_svector_ostream>(rso, type);
91+
return exportType.str().str();
92+
}
93+
8394
template <typename StdIOpTy, typename V1, V1 v1, typename StdFOpTy, typename V2, V2 v2>
8495
mlir::Value LogicOp(Operation *binOp, SyntaxKind op, mlir::Value left, mlir::Type leftType, mlir::Value right, mlir::Type rightType,
8596
PatternRewriter &builder, const LLVMTypeConverter &typeConverter, CompileOptions &compileOptions)
@@ -139,8 +150,8 @@ mlir::Value LogicOp(Operation *binOp, SyntaxKind op, mlir::Value left, mlir::Typ
139150

140151
return value;
141152
}
142-
else if (isa<mlir_ts::AnyType>(leftType) || isa<mlir_ts::ClassType>(leftType) ||
143-
isa<mlir_ts::OpaqueType>(leftType) || isa<mlir_ts::NullType>(leftType))
153+
else if (isa<mlir_ts::AnyType>(leftType) || isa<mlir_ts::ClassType>(leftType) ||
154+
isa<mlir_ts::OpaqueType>(leftType) || isa<mlir_ts::NullType>(leftType))
144155
{
145156
// excluded string
146157
auto intPtrType = llvmtch.getIntPtrType(0);
@@ -208,14 +219,25 @@ mlir::Value LogicOp(Operation *binOp, SyntaxKind op, mlir::Value left, mlir::Typ
208219
mlir::Value rightPtrValue = builder.create<LLVM::PtrToIntOp>(loc, intPtrType, rightArrayPtrValueAsLLVMType);
209220

210221
auto value = builder.create<StdIOpTy>(loc, v1, leftPtrValue, rightPtrValue);
211-
return value;
222+
return value;
212223
}
224+
else if (auto unionType = dyn_cast<mlir_ts::UnionType>(leftType))
225+
{
226+
::typescript::MLIRTypeHelper mth(builder.getContext(), compileOptions);
227+
mlir::Type baseType;
228+
if (mth.isUnionTypeNeedsTag(loc, unionType, baseType))
229+
{
230+
emitError(loc, "Not applicable logical operator for type: '") << to_print<int>(leftType) << "'";
231+
}
232+
233+
return LogicOp<StdIOpTy, V1, v1, StdFOpTy, V2, v2>(binOp, op, left, baseType, right, rightType, builder, typeConverter, compileOptions);
234+
}
213235
else if (auto leftTupleType = dyn_cast<mlir_ts::TupleType>(leftType))
214236
{
215237
// TODO: finish comparing 2 the same tuples
216238
}
217239

218-
emitWarning(loc, "Not applicable logical operator for type: '") << leftType << "'";
240+
emitWarning(loc, "Not applicable logical operator for type: '") << to_print<int>(leftType) << "'";
219241
// false by default
220242
CodeLogicHelper clh(binOp, builder);
221243
return clh.createI1ConstantOf(false);

tsc/include/TypeScript/MLIRLogic/MLIRDebugInfoHelper.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class MLIRDebugInfoHelper
124124

125125
if (auto file = dyn_cast_or_null<mlir::LLVM::DIFileAttr>(debugScope.lookup(FILE_DEBUG_SCOPE)))
126126
{
127-
unsigned sourceLanguage = llvm::dwarf::DW_LANG_C_plus_plus;
127+
unsigned sourceLanguage = llvm::dwarf::DW_LANG_Assembly;
128128
auto producer = builder.getStringAttr(producerName);
129129
auto emissionKind = mlir::LLVM::DIEmissionKind::Full;
130130
auto namedTable = mlir::LLVM::DINameTableKind::Default;
@@ -146,8 +146,11 @@ class MLIRDebugInfoHelper
146146
{
147147
if (auto scopeAttr = dyn_cast_or_null<mlir::LLVM::DIScopeAttr>(debugScope.lookup(DEBUG_SCOPE)))
148148
{
149-
auto [line, column] = LocationHelper::getLineAndColumn(functionLocation);
150-
auto [scopeLine, scopeColumn] = LocationHelper::getLineAndColumn(functionBlockLocation);
149+
LocationHelper lh(builder.getContext());
150+
auto [file, lineAndColumn] = lh.getLineAndColumnAndFile(functionLocation);
151+
auto [line, column] = lineAndColumn;
152+
auto [scopeFile, scopeLineAndColumn] = lh.getLineAndColumnAndFile(functionBlockLocation);
153+
auto [scopeLine, scopeColumn] = scopeLineAndColumn;
151154

152155
// if (isa<mlir::LLVM::DILexicalBlockAttr>(scopeAttr))
153156
// {
@@ -178,7 +181,7 @@ class MLIRDebugInfoHelper
178181
auto subprogramAttr = mlir::LLVM::DISubprogramAttr::get(
179182
builder.getContext(), DistinctAttr::create(builder.getUnitAttr()), compileUnitAttr, scopeAttr,
180183
funcNameAttr, linkageNameAttr,
181-
compileUnitAttr.getFile(), line, scopeLine, subprogramFlags, type);
184+
file/*compileUnitAttr.getFile()*/, line, scopeLine, subprogramFlags, type);
182185

183186
debugScope.insert(SUBPROGRAM_DEBUG_SCOPE, subprogramAttr);
184187
debugScope.insert(DEBUG_SCOPE, subprogramAttr);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef MLIR_TYPESCRIPT_COMMONGENLOGIC_MLIRTYPECORE_H_
2+
#define MLIR_TYPESCRIPT_COMMONGENLOGIC_MLIRTYPECORE_H_
3+
4+
#include "TypeScript/TypeScriptOps.h"
5+
6+
#include "llvm/Support/Debug.h"
7+
8+
#define DEBUG_TYPE "mlir"
9+
10+
namespace mlir_ts = mlir::typescript;
11+
12+
namespace typescript
13+
{
14+
15+
class MLIRTypeCore
16+
{
17+
public:
18+
static bool isNullableType(mlir::Type typeIn)
19+
{
20+
if (isa<mlir_ts::NullType>(typeIn)
21+
|| isa<mlir_ts::StringType>(typeIn)
22+
|| isa<mlir_ts::ObjectType>(typeIn)
23+
|| isa<mlir_ts::ClassType>(typeIn)
24+
|| isa<mlir_ts::InterfaceType>(typeIn)
25+
|| isa<mlir_ts::AnyType>(typeIn)
26+
|| isa<mlir_ts::UnknownType>(typeIn)
27+
|| isa<mlir_ts::RefType>(typeIn)
28+
|| isa<mlir_ts::ValueRefType>(typeIn))
29+
{
30+
return true;
31+
}
32+
33+
if (auto unionType = dyn_cast<mlir_ts::UnionType>(typeIn))
34+
{
35+
return llvm::any_of(unionType.getTypes(), [&](mlir::Type t) { return isNullableType(t); });
36+
}
37+
38+
return false;
39+
}
40+
41+
// TODO: should be in separate static class
42+
static bool isNullableOrOptionalType(mlir::Type typeIn)
43+
{
44+
if (isa<mlir_ts::NullType>(typeIn)
45+
|| isa<mlir_ts::UndefinedType>(typeIn)
46+
|| isa<mlir_ts::StringType>(typeIn)
47+
|| isa<mlir_ts::ObjectType>(typeIn)
48+
|| isa<mlir_ts::ClassType>(typeIn)
49+
|| isa<mlir_ts::InterfaceType>(typeIn)
50+
|| isa<mlir_ts::OptionalType>(typeIn)
51+
|| isa<mlir_ts::AnyType>(typeIn)
52+
|| isa<mlir_ts::UnknownType>(typeIn)
53+
|| isa<mlir_ts::RefType>(typeIn)
54+
|| isa<mlir_ts::ValueRefType>(typeIn))
55+
{
56+
return true;
57+
}
58+
59+
if (auto unionType = dyn_cast<mlir_ts::UnionType>(typeIn))
60+
{
61+
return llvm::any_of(unionType.getTypes(), [&](mlir::Type t) { return isNullableOrOptionalType(t); });
62+
}
63+
64+
return false;
65+
}
66+
};
67+
68+
} // namespace typescript
69+
70+
#undef DEBUG_TYPE
71+
72+
#endif // MLIR_TYPESCRIPT_COMMONGENLOGIC_MLIRTYPECORE_H_

tsc/include/TypeScript/MLIRLogic/MLIRTypeHelper.h

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "TypeScript/MLIRLogic/MLIRTypeIterator.h"
99
#include "TypeScript/MLIRLogic/MLIRHelper.h"
1010
#include "TypeScript/MLIRLogic/MLIRPrinter.h"
11+
#include "TypeScript/MLIRLogic/MLIRTypeCore.h"
1112

1213
#include "llvm/Support/Debug.h"
1314
#include "llvm/ADT/APSInt.h"
@@ -190,8 +191,8 @@ class MLIRTypeHelper
190191
bool isValueType(mlir::Type typeIn)
191192
{
192193
auto type = getBaseType(typeIn);
193-
return type && (type.isIntOrIndexOrFloat() || isa<mlir_ts::NumberType>(type) || isa<mlir_ts::BooleanType>(type) ||
194-
isa<mlir_ts::TupleType>(type) || isa<mlir_ts::ConstTupleType>(type) || isa<mlir_ts::ConstArrayType>(type));
194+
return type && (type.isIntOrIndexOrFloat() || isa<mlir_ts::NumberType>(type) || isa<mlir_ts::BooleanType>(type) || isa<mlir_ts::CharType>(type) ||
195+
isa<mlir_ts::TupleType>(type) || isa<mlir_ts::ConstTupleType>(type) || isa<mlir_ts::ConstArrayType>(type));
195196
}
196197

197198
bool isNumericType(mlir::Type type)
@@ -217,31 +218,6 @@ class MLIRTypeHelper
217218
return elementType;
218219
}
219220

220-
bool isNullableOrOptionalType(mlir::Type typeIn)
221-
{
222-
if (isa<mlir_ts::NullType>(typeIn)
223-
|| isa<mlir_ts::UndefinedType>(typeIn)
224-
|| isa<mlir_ts::StringType>(typeIn)
225-
|| isa<mlir_ts::ObjectType>(typeIn)
226-
|| isa<mlir_ts::ClassType>(typeIn)
227-
|| isa<mlir_ts::InterfaceType>(typeIn)
228-
|| isa<mlir_ts::OptionalType>(typeIn)
229-
|| isa<mlir_ts::AnyType>(typeIn)
230-
|| isa<mlir_ts::UnknownType>(typeIn)
231-
|| isa<mlir_ts::RefType>(typeIn)
232-
|| isa<mlir_ts::ValueRefType>(typeIn))
233-
{
234-
return true;
235-
}
236-
237-
if (auto unionType = dyn_cast<mlir_ts::UnionType>(typeIn))
238-
{
239-
return llvm::any_of(unionType.getTypes(), [&](mlir::Type t) { return isNullableOrOptionalType(t); });
240-
}
241-
242-
return false;
243-
}
244-
245221
mlir::Type getElementTypeOrSelf(mlir::Type type)
246222
{
247223
return MLIRHelper::getElementTypeOrSelf(type);

tsc/include/TypeScript/MLIRLogic/TypeOfOpHelper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ class TypeOfOpHelper
203203
return typeOfValue;
204204
}
205205

206+
if (isa<mlir_ts::CharType>(type))
207+
{
208+
auto typeOfValue = strValue(loc, "char");
209+
return typeOfValue;
210+
}
211+
206212
LLVM_DEBUG(llvm::dbgs() << "TypeOf: " << type << "\n");
207213

208214
llvm_unreachable("not implemented");

tsc/include/TypeScript/TypeScriptOps.td

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ def ConstantOp : TypeScript_Op<"Constant",
4444
];
4545

4646
let hasFolder = 1;
47-
48-
4947
}
5048

5149
def SymbolRefOp : TypeScript_Op<"SymbolRef",
@@ -1495,6 +1493,20 @@ def TypeScript_CastOp : TypeScript_Op<"Cast", [DeclareOpInterfaceMethods<CastOpI
14951493
let hasVerifier = 1;
14961494
}
14971495

1496+
def TypeScript_SafeCastOp : TypeScript_Op<"SafeCast", [
1497+
DeclareOpInterfaceMethods<CastOpInterface>,
1498+
AllTypesMatch<["safe", "res"]>,
1499+
Pure]> {
1500+
let description = [{
1501+
Example:
1502+
ts.safe_cast %v : i32 to f16
1503+
}];
1504+
let arguments = (ins AnyType:$safe, AnyType:$value);
1505+
let results = (outs AnyType:$res);
1506+
1507+
let hasFolder = 1;
1508+
}
1509+
14981510
def TypeScript_BoxOp : TypeScript_Op<"Box", [Pure]> {
14991511
let description = [{
15001512
Example:

0 commit comments

Comments
 (0)