Skip to content

Commit 04a0440

Browse files
general set of fixes (#128)
* improvements * restore * progress of fixes * progress of adding set lenght for string * added set length for string to pre allocate data * added typed sizeof * added test for generic sizeof * fix issue with printing object without field names * more fixes to use {} * reefactored using enumerate * refactoring finished and convert from const array to string is done * added fix for generating debug info for index type * more fixes * fixes to int long types * fixes after refactirng using enumerate * fixed issues with using optional type with union types * fixed issue with safe cast for undefined values * added safe cast for conditional expressions and optional values * added test for safe cast in conditional expressions for optional values * fix for safecast ops * fixes * fixed recusive conditional expressions * progress of adding safe cast to logical ops * improving cast of native types * progress of implementing simpification for IfOp and LogicalBinOp * fixes to simplification of LogicalOp * update * fixed issue with safe-casting from union type into smaller union type * update * progress * fixes to access to array if data is optional * more fixes to access optional type * added test for arrray access for optional type * fixes to array access * progress of implementing ref for optional types * finished refactoring loadOp to reference * progress * progress * fixes * fixed issue with optional array type * added warning for undefined types * update build script
1 parent 9f2d63d commit 04a0440

24 files changed

+815
-347
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-alpha53 -m "pre alpha v0.0-53"
1+
git tag -a v0.0-pre-alpha54 -m "pre alpha v0.0-54"
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-alpha53
2-
git tag -d v0.0-pre-alpha53
1+
git push --delete origin v0.0-pre-alpha54
2+
git tag -d v0.0-pre-alpha54

tsc/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ include_directories(${PROJECT_BINARY_DIR}/include)
119119
link_directories(${LLVM_BUILD_LIBRARY_DIR})
120120
add_definitions(${LLVM_DEFINITIONS})
121121

122-
add_definitions(-D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING)
122+
if (WIN32)
123+
else()
124+
# linux
125+
add_definitions(-D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING)
126+
endif()
123127

124128
# enabling testing
125129
enable_testing()

tsc/include/TypeScript/LowerToLLVM/CastLogicHelper.h

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "TypeScript/LowerToLLVM/AnyLogic.h"
1919
#include "TypeScript/LowerToLLVM/LLVMCodeHelperBase.h"
2020

21+
#include "mlir/Dialect/Index/IR/IndexDialect.h"
22+
#include "mlir/Dialect/Index/IR/IndexOps.h"
2123
#include "mlir/Dialect/Arith/IR/Arith.h"
2224

2325
using namespace mlir;
@@ -92,14 +94,14 @@ class CastLogicHelper
9294
return castBoolToString(in);
9395
}
9496

95-
if (inType.isa<mlir::IndexType>() && isResString)
97+
if (inType.isIndex() && isResString)
9698
{
97-
return castIntToString(in, inLLVMType.getIntOrFloatBitWidth(), false);
99+
return castIntToString(in, tch.getIndexTypeBitwidth(), false);
98100
}
99101

100-
if (inLLVMType.isIntOrIndex() && inType.isa<mlir::IntegerType>() && isResString)
102+
if (inType.isa<mlir::IntegerType>() && isResString)
101103
{
102-
return castIntToString(in, inLLVMType.getIntOrFloatBitWidth(), inType.cast<mlir::IntegerType>().isSignedInteger());
104+
return castIntToString(in, inLLVMType.getIntOrFloatBitWidth(), inType.isSignedInteger());
103105
}
104106

105107
if ((inLLVMType.isF16() || inLLVMType.isF32() || inLLVMType.isF64() || inLLVMType.isF128()) && isResString)
@@ -120,7 +122,19 @@ class CastLogicHelper
120122
return castF64ToString(in);
121123
}
122124

123-
if (inType.isIntOrIndex() && resType.isSignedInteger() && resType.getIntOrFloatBitWidth() > inType.getIntOrFloatBitWidth())
125+
if (inType.isIndex())
126+
{
127+
if (resType.isSignedInteger())
128+
{
129+
return rewriter.create<mlir::index::CastSOp>(loc, resLLVMType, in);
130+
}
131+
else
132+
{
133+
return rewriter.create<mlir::index::CastUOp>(loc, resLLVMType, in);
134+
}
135+
}
136+
137+
if (inType.isSignedInteger() && resType.isSignedInteger() && resType.getIntOrFloatBitWidth() > inType.getIntOrFloatBitWidth())
124138
{
125139
return rewriter.create<LLVM::SExtOp>(loc, resLLVMType, in);
126140
}
@@ -709,11 +723,9 @@ class CastLogicHelper
709723

710724
LLVM_DEBUG(llvm::dbgs() << "invalid cast operator type 1: '" << inLLVMType << "', type 2: '" << resLLVMType << "'\n";);
711725

712-
// emitError(loc, "invalid cast from ") << inLLVMType << " to " << resLLVMType;
713-
714-
// return mlir::Value();
715-
emitWarning(loc, "invalid cast from ") << inLLVMType << " to " << resLLVMType;
716-
return rewriter.create<LLVM::UndefOp>(loc, resLLVMType);
726+
emitError(loc, "invalid cast from ") << inLLVMType << " to " << resLLVMType;
727+
//return rewriter.create<LLVM::UndefOp>(loc, resLLVMType);
728+
return mlir::Value();
717729
}
718730

719731
mlir::Value castTupleToTuple(mlir::Value in, ::llvm::ArrayRef<::mlir::typescript::FieldInfo> fields, mlir_ts::TupleType tupleTypeRes)
@@ -750,7 +762,6 @@ class CastLogicHelper
750762
};
751763

752764
// map values
753-
auto count = fields.size();
754765
auto dstIndex = -1;
755766
for (auto destField : tupleTypeRes.getFields())
756767
{
@@ -770,9 +781,8 @@ class CastLogicHelper
770781

771782
auto found = false;
772783
auto anyFieldWithName = false;
773-
for (auto index = 0; index < count; index++)
784+
for (auto [index, srcField] : enumerate(fields))
774785
{
775-
auto srcField = fields[index];
776786
if (!srcField.id)
777787
{
778788
continue;

tsc/include/TypeScript/LowerToLLVM/LLVMDebugInfo.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,8 @@ class LLVMDebugInfoHelper
232232
CompositeSizesTrack sizesTrack(llvmtch);
233233

234234
llvm::SmallVector<LLVM::DINodeAttr> elements;
235-
auto index = -1;
236-
for (auto llvmElementType : structType.getBody())
235+
for (auto [index, llvmElementType] : enumerate(structType.getBody()))
237236
{
238-
index++;
239-
240237
sizesTrack.nextElementType(llvmElementType);
241238

242239
// name

tsc/include/TypeScript/LowerToLLVM/LLVMRTTIHelperVCWin32.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,9 @@ class LLVMRTTIHelperVCWin32
406406
// make array
407407
mlir::Value arrayVal = rewriter.create<LLVM::UndefOp>(loc, th.getArrayType(th.getI32Type(), arraySize));
408408

409-
auto index = 0;
410-
for (auto value : values)
409+
for (auto [index, value] : enumerate(values))
411410
{
412-
ch.setStructValue(loc, arrayVal, value, index++);
411+
ch.setStructValue(loc, arrayVal, value, index);
413412
}
414413

415414
// [size, {values...}]

tsc/include/TypeScript/LowerToLLVM/TypeConverterHelper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "TypeScript/TypeScriptOps.h"
99

1010
#include "mlir/Transforms/DialectConversion.h"
11+
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
1112

1213
using namespace mlir;
1314
namespace mlir_ts = mlir::typescript;
@@ -46,6 +47,11 @@ class TypeConverterHelper
4647

4748
llvm_unreachable("not implemented");
4849
}
50+
51+
int getIndexTypeBitwidth()
52+
{
53+
return (*(mlir::LLVMTypeConverter *)&typeConverter).getIndexTypeBitwidth();
54+
}
4955
};
5056
} // namespace typescript
5157

tsc/include/TypeScript/LowerToLLVM/TypeOfOpHelper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class TypeOfOpHelper
5252

5353
if (type.isIndex())
5454
{
55-
auto typeOfValue = strValue(loc, "ptrint");
55+
auto typeOfValue = strValue(loc, "index");
5656
return typeOfValue;
5757
}
5858

@@ -81,6 +81,12 @@ class TypeOfOpHelper
8181
return typeOfValue;
8282
}
8383

84+
if (type.isa<mlir_ts::CharType>())
85+
{
86+
auto typeOfValue = strValue(loc, "char");
87+
return typeOfValue;
88+
}
89+
8490
if (type.isa<mlir_ts::ArrayType>())
8591
{
8692
auto typeOfValue = strValue(loc, "array");

tsc/include/TypeScript/MLIRLogic/MLIRCodeLogic.h

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ namespace typescript
2626
class MLIRCodeLogic
2727
{
2828
mlir::MLIRContext *context;
29+
mlir::OpBuilder builder;
2930

3031
public:
31-
MLIRCodeLogic(mlir::MLIRContext *context) : context(context)
32+
MLIRCodeLogic(mlir::MLIRContext *context) : context(context), builder(context)
3233
{
3334
}
3435

35-
MLIRCodeLogic(mlir::OpBuilder builder) : context(builder.getContext())
36+
MLIRCodeLogic(mlir::OpBuilder builder) : context(builder.getContext()), builder(builder)
3637
{
3738
}
3839

@@ -54,14 +55,36 @@ class MLIRCodeLogic
5455
return mlir::Attribute();
5556
}
5657

57-
mlir::Value GetReferenceOfLoadOp(mlir::Value value)
58+
mlir::Value GetReferenceOfLoadOp(mlir::Value object)
5859
{
59-
// TODO: sync with Common Logic
60-
if (auto loadOp = dyn_cast<mlir_ts::LoadOp>(value.getDefiningOp()))
60+
if (auto loadOp = object.getDefiningOp<mlir_ts::LoadOp>())
6161
{
62-
// this LoadOp will be removed later as unused
63-
auto refValue = loadOp.getReference();
64-
return refValue;
62+
// get PropertyRef out of extractPropertyOp
63+
return loadOp.getReference();
64+
}
65+
66+
if (auto valueOp = object.getDefiningOp<mlir_ts::ValueOp>())
67+
{
68+
if (auto nestedRef = GetReferenceOfLoadOp(valueOp.getIn()))
69+
{
70+
return builder.create<mlir_ts::PropertyRefOp>(
71+
object.getLoc(),
72+
mlir_ts::RefType::get(valueOp.getType()),
73+
nestedRef,
74+
OPTIONAL_VALUE_INDEX);
75+
}
76+
}
77+
78+
if (auto extractPropertyOp = object.getDefiningOp<mlir_ts::ExtractPropertyOp>())
79+
{
80+
if (auto nestedRef = GetReferenceOfLoadOp(extractPropertyOp.getObject()))
81+
{
82+
return builder.create<mlir_ts::PropertyRefOp>(
83+
object.getLoc(),
84+
mlir_ts::RefType::get(extractPropertyOp.getType()),
85+
nestedRef,
86+
extractPropertyOp.getPosition().front());
87+
}
6588
}
6689

6790
return mlir::Value();
@@ -232,7 +255,7 @@ class MLIRCustomMethods
232255
return m[functionName.str()];
233256
}
234257

235-
ValueOrLogicalResult callMethod(StringRef functionName, ArrayRef<mlir::Value> operands, std::function<ValueOrLogicalResult(mlir::Location, mlir::Type, mlir::Value, const GenContext &)> castFn, const GenContext &genContext)
258+
ValueOrLogicalResult callMethod(StringRef functionName, mlir::SmallVector<mlir::Type> typeArgs, ArrayRef<mlir::Value> operands, std::function<ValueOrLogicalResult(mlir::Location, mlir::Type, mlir::Value, const GenContext &)> castFn, const GenContext &genContext)
236259
{
237260
if (functionName == "print")
238261
{
@@ -264,7 +287,7 @@ class MLIRCustomMethods
264287
}
265288
else if (functionName == "sizeof")
266289
{
267-
return mlirGenSizeOf(location, operands);
290+
return mlirGenSizeOf(location, typeArgs, operands);
268291
}
269292
else if (functionName == "__array_push")
270293
{
@@ -326,29 +349,8 @@ class MLIRCustomMethods
326349
{
327350
if (!oper.getType().isa<mlir_ts::StringType>())
328351
{
329-
if (oper.getType().isa<mlir_ts::OptionalType>())
330-
{
331-
auto hasValue = builder.create<mlir_ts::HasValueOp>(location, mlir_ts::BooleanType::get(builder.getContext()), oper);
332-
MLIRCodeLogicHelper mclh(builder, location);
333-
334-
auto strType = mlir_ts::StringType::get(builder.getContext());
335-
auto optValue = mclh.conditionalExpression(
336-
strType, hasValue,
337-
[&](mlir::OpBuilder &builder, mlir::Location location) {
338-
return builder.create<mlir_ts::CastOp>(location, strType, oper);
339-
},
340-
[&](mlir::OpBuilder &builder, mlir::Location location) {
341-
return builder.create<mlir_ts::ConstantOp>(
342-
location, strType, builder.getStringAttr(UNDEFINED_NAME));
343-
});
344-
345-
vals.push_back(optValue);
346-
}
347-
else
348-
{
349-
auto strCast = castFn(location, mlir_ts::StringType::get(builder.getContext()), oper, genContext);
350-
vals.push_back(strCast);
351-
}
352+
auto strCast = castFn(location, mlir_ts::StringType::get(builder.getContext()), oper, genContext);
353+
vals.push_back(strCast);
352354
}
353355
else
354356
{
@@ -491,12 +493,19 @@ class MLIRCustomMethods
491493
return isNaNOp;
492494
}
493495

494-
mlir::Value mlirGenSizeOf(const mlir::Location &location, ArrayRef<mlir::Value> operands)
496+
mlir::Value mlirGenSizeOf(const mlir::Location &location, mlir::SmallVector<mlir::Type> typeArgs, ArrayRef<mlir::Value> operands)
495497
{
496-
auto sizeOfValue = builder.create<mlir_ts::SizeOfOp>(location, builder.getIndexType(),
497-
mlir::TypeAttr::get(operands.front().getType()));
498+
if (typeArgs.size() > 0)
499+
{
500+
return builder.create<mlir_ts::SizeOfOp>(location, builder.getIndexType(), mlir::TypeAttr::get(typeArgs.front()));
501+
}
498502

499-
return sizeOfValue;
503+
if (operands.size() > 0)
504+
{
505+
return builder.create<mlir_ts::SizeOfOp>(location, builder.getIndexType(), mlir::TypeAttr::get(operands.front().getType()));
506+
}
507+
508+
return mlir::Value();
500509
}
501510

502511
ValueOrLogicalResult mlirGenArrayPush(const mlir::Location &location, mlir::Value thisValue, ArrayRef<mlir::Value> values)

tsc/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCLinux.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,23 @@ class MLIRRTTIHelperVCLinux
9191
{
9292
auto first = true;
9393
auto countM1 = names.size() - 1;
94-
auto index = 0;
95-
for (auto name : names)
94+
for (auto [index, name] : enumerate(names))
9695
{
9796
if (first)
9897
{
9998
types.push_back({name.str(), TypeInfo::Pointer_TypeInfo, 1});
10099
}
101100

102-
types.push_back({name.str(), index < countM1 ? TypeInfo::SingleInheritance_ClassTypeInfo : TypeInfo::ClassTypeInfo,
103-
index < countM1 ? index + 2 : -1});
101+
if (index < countM1)
102+
{
103+
types.push_back({ name.str(), TypeInfo::SingleInheritance_ClassTypeInfo, (int)index + 2 });
104+
}
105+
else
106+
{
107+
types.push_back({ name.str(), TypeInfo::ClassTypeInfo, -1 });
108+
}
104109

105110
first = false;
106-
index++;
107111
}
108112
}
109113

tsc/include/TypeScript/MLIRLogic/MLIRRTTIHelperVCWin32.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,9 @@ class MLIRRTTIHelperVCWin32
528528
// make array
529529
mlir::Value arrayVal = rewriter.create<mlir_ts::UndefOp>(loc, mth.getConstArrayValueType(mth.getI32Type(), arraySize));
530530

531-
auto index = 0;
532-
for (auto value : values)
531+
for (auto [index, value] : enumerate(values))
533532
{
534-
setStructValue(loc, arrayVal, value, index++);
533+
setStructValue(loc, arrayVal, value, index);
535534
}
536535

537536
// [size, {values...}]

0 commit comments

Comments
 (0)