summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-02-27 15:17:13 -0500
committerGitHub <noreply@github.com>2019-02-27 15:17:13 -0500
commiteee68205636f26ac8060dbb2b1725fd3c80e54a8 (patch)
treeb07d0be17046a763d80b04b211dd6869bff2bd9d
parent8ac4452754292b74fa6bf2713dd9f1458ce5243d (diff)
Hotfix/fix stdlib error reporting (#866)
* * Add 'identity' version of bit casts (asint, asuint, asfloat) for scalar and vector * Added identity bit casts for matrix (cos no op). We don't support matrix asint on glsl targets * Added tests in bit-cast.slang * Use kIRPseudoOp_Pos for identity asuint/asint/asfloat casts. * * Stop crash if error in stdlib * Use the buildin source manager when compiling stdlib - fixes that line numbers are displayed * Typo fix * Output line directives for 'meta' slang souce files into stdlib * Improve comments and function names.
-rw-r--r--source/slang/compiler.h2
-rw-r--r--source/slang/core.meta.slang.h4
-rw-r--r--source/slang/emit.cpp4
-rw-r--r--source/slang/hlsl.meta.slang.h5
-rw-r--r--source/slang/slang.cpp8
-rw-r--r--tools/slang-generate/main.cpp109
6 files changed, 119 insertions, 13 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index 246b34ea7..49d777931 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -706,7 +706,7 @@ namespace Slang
{
// TODO: We really shouldn't need this type in the long run.
// The few places that rely on it should be refactored to just
- // depend on the unerlying information (a linkage and a diagnostic
+ // depend on the underlying information (a linkage and a diagnostic
// sink) directly.
//
// The flags to control dumping and validation of IR should be
diff --git a/source/slang/core.meta.slang.h b/source/slang/core.meta.slang.h
index 2c59689a2..25feb0005 100644
--- a/source/slang/core.meta.slang.h
+++ b/source/slang/core.meta.slang.h
@@ -142,6 +142,7 @@ for (int tt = 0; tt < kBaseTypeCount; ++tt)
// TODO: should this cover the full gamut of integer types?
case BaseType::Int:
case BaseType::UInt:
+SLANG_RAW("#line 145 \"core.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW(" __generic<T:__EnumType>\n")
SLANG_RAW(" __init(T value);\n")
@@ -157,6 +158,7 @@ SLANG_RAW(" __init(T value);\n")
// Declare built-in pointer type
// (eventually we can have the traditional syntax sugar for this)
+SLANG_RAW("#line 160 \"core.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("__generic<T>\n")
@@ -218,6 +220,7 @@ sb << " __init(T value);\n";
sb << " __init(vector<T,N> value);\n";
sb << "};\n";
+SLANG_RAW("#line 206 \"core.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("__generic<T = float, let R : int = 4, let C : int = 4>\n")
@@ -1194,6 +1197,7 @@ for (auto op : binaryOps)
sb << "__intrinsic_op(" << int(op.opCode) << ") matrix<" << resultType << ",N,M> operator" << op.opName << "(" << leftQual << "matrix<" << leftType << ",N,M> left, " << rightType << " right);\n";
}
}
+SLANG_RAW("#line 1182 \"core.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("// Operators to apply to `enum` types\n")
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp
index 6260ac218..815f27bfe 100644
--- a/source/slang/emit.cpp
+++ b/source/slang/emit.cpp
@@ -6970,8 +6970,8 @@ String emitEntryPoint(
String code = sharedContext.sb.ProduceString();
sharedContext.sb.Clear();
- // Now that we've emitted the code for all the declaratiosn in the file,
- // it is time to stich together the final output.
+ // Now that we've emitted the code for all the declarations in the file,
+ // it is time to stitch together the final output.
// There may be global-scope modifiers that we should emit now
visitor.emitGLSLPreprocessorDirectives();
diff --git a/source/slang/hlsl.meta.slang.h b/source/slang/hlsl.meta.slang.h
index e26269c3c..375c8aab0 100644
--- a/source/slang/hlsl.meta.slang.h
+++ b/source/slang/hlsl.meta.slang.h
@@ -119,6 +119,7 @@ static const struct {
{ kIROp_HLSLRasterizerOrderedByteAddressBufferType, "RasterizerOrderedByteAddressBuffer" },
};
for(auto item : kMutableByteAddressBufferCases) {
+SLANG_RAW("#line 104 \"hlsl.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("__magic_type(HLSL")
@@ -271,6 +272,7 @@ SLANG_RAW("};\n")
SLANG_RAW("\n")
}
+SLANG_RAW("#line 247 \"hlsl.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
@@ -283,6 +285,7 @@ static const struct {
{ kIROp_HLSLRasterizerOrderedStructuredBufferType, "RasterizerOrderedStructuredBuffer" },
};
for(auto item : kMutableStructuredBufferCases) {
+SLANG_RAW("#line 259 \"hlsl.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("\n")
@@ -320,6 +323,7 @@ SLANG_RAW("};\n")
SLANG_RAW("\n")
}
+SLANG_RAW("#line 287 \"hlsl.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("__generic<T>\n")
@@ -1509,6 +1513,7 @@ for (int aa = 0; aa < kBaseBufferAccessLevelCount; ++aa)
sb << "};\n";
}
+SLANG_RAW("#line 1440 \"hlsl.meta.slang\"")
SLANG_RAW("\n")
SLANG_RAW("\n")
SLANG_RAW("\n")
diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp
index 45834c31d..0bba68aa6 100644
--- a/source/slang/slang.cpp
+++ b/source/slang/slang.cpp
@@ -1488,6 +1488,14 @@ void Session::addBuiltinSource(
m_builtinLinkage,
&sink);
+ SourceManager* sourceManager = getBuiltinSourceManager();
+
+ // Set the source manager on the sink
+ sink.sourceManager = sourceManager;
+ // Make the linkage use the builtin source manager
+ Linkage* linkage = compileRequest->getLinkage();
+ linkage->setSourceManager(sourceManager);
+
Name* moduleName = getNamePool()->getName(path);
auto translationUnitIndex = compileRequest->addTranslationUnit(SourceLanguage::Slang, moduleName);
diff --git a/tools/slang-generate/main.cpp b/tools/slang-generate/main.cpp
index 78097d1f1..a2fec6578 100644
--- a/tools/slang-generate/main.cpp
+++ b/tools/slang-generate/main.cpp
@@ -35,6 +35,14 @@ struct Node
Node* next;
};
+// Information about a source file
+struct SourceFile
+{
+ char const* inputPath;
+ StringSpan text;
+ Node* node;
+};
+
void addNode(
Node**& ioLink,
Node::Flavor flavor,
@@ -593,12 +601,99 @@ void emitCodeNodes(
}
}
+// Given line starts and a location, find the line number. Returns -1 if not found
+static int _findLineIndex(const List<const char*>& lineBreaks, const char* location)
+{
+ if (location == nullptr)
+ {
+ return -1;
+ }
+
+ // Use a binary chop to find the associated line
+ int lo = 0;
+ int hi = int(lineBreaks.Count());
+
+ while (lo + 1 < hi)
+ {
+ const int mid = (hi + lo) >> 1;
+ const auto midOffset = lineBreaks[mid];
+ if (midOffset <= location)
+ {
+ lo = mid;
+ }
+ else
+ {
+ hi = mid;
+ }
+ }
+
+ return lo;
+}
+
+static void _calcLineBreaks(const UnownedStringSlice& content, List<const char*>& outLineStarts)
+{
+ char const* begin = content.begin();
+ char const* end = content.end();
+
+ char const* cursor = begin;
+
+ // Treat the beginning of the file as a line break
+ outLineStarts.Add(cursor);
+
+ while (cursor != end)
+ {
+ int c = *cursor++;
+ switch (c)
+ {
+ case '\r': case '\n':
+ {
+ // When we see a line-break character we need
+ // to record the line break, but we also need
+ // to deal with the annoying issue of encodings,
+ // where a multi-byte sequence might encode
+ // the line break.
+
+ int d = *cursor;
+ if ((c^d) == ('\r' ^ '\n'))
+ cursor++;
+
+ outLineStarts.Add(cursor);
+ break;
+ }
+ default:
+ break;
+ }
+ }
+}
+
void emitTemplateNodes(
+ SourceFile* sourceFile,
FILE* stream,
Node* node)
{
- for (auto nn = node; nn; nn = nn->next)
+ // Work out
+ List<const char*> lineBreaks;
+ _calcLineBreaks(sourceFile->text, lineBreaks);
+
+ Node* prev = nullptr;
+ for (auto nn = node; nn; prev = nn, nn = nn->next)
{
+ // If we transition from escape to text, insert line number directive
+ bool enable = true;
+ if (enable && prev && prev->flavor == Node::Flavor::escape && nn->flavor == Node::Flavor::text)
+ {
+ // Find the line
+ int lineIndex = _findLineIndex(lineBreaks, nn->span.begin());
+ // If found, output the directive
+ if (lineIndex >= 0)
+ {
+ StringBuilder buf;
+ buf << "SLANG_RAW(\"#line " << (lineIndex + 1) << " \\\"" << sourceFile->inputPath << "\\\"\")\n";
+
+ emit(stream, buf.getUnownedSlice());
+ }
+ }
+
switch (nn->flavor)
{
case Node::Flavor::text:
@@ -616,7 +711,7 @@ void emitTemplateNodes(
case Node::Flavor::escape:
emitCodeNodes(stream, nn->body);
break;
- }
+ }
}
}
@@ -691,13 +786,7 @@ PARSE_HANDLER(parseUnknownFile)
return nullptr;
}
-// Information about a source file
-struct SourceFile
-{
- char const* inputPath;
- StringSpan text;
- Node* node;
-};
+
Node* parseSourceFile(SourceFile* file)
{
@@ -810,7 +899,7 @@ int main(
FILE* outputStream;
fopen_s(&outputStream, outputPath.Buffer(), "w");
- emitTemplateNodes(outputStream, node);
+ emitTemplateNodes(sourceFile, outputStream, node);
fclose(outputStream);