From 35318fb2b08c82f80cbd464e93d81ebe719c40be Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Wed, 20 Dec 2017 14:06:59 -0800 Subject: More fixes for Falcor IR support (#317) * Fix: try to improve float literal formatting An earlier change switched to using the C++ stdlib to format floats, but I neglected to check that it would correctly print values that happen to be integral with a decimal place (e.g., print `1.0` instead of just `1`). That causes some obscure cases to fail (e.g., when you write `1.0.xxxx` in HLSL, and we print out `1.xxxx`). I've tried to resolve the issue by using the "fixed" output format, but that may also create problems for extremely large or small values (which really need to use scientific notation). However, this at least puts us back where we were before... * Add support for source locations to the IR - Add a `sourceLocation` field to all `IRValue`s - Add some logic to associate locations with operations while lowering (using a slightly "clever" RAII approach) - Make sure that when cloning instructions, we also clone the location - Make the emit logic use the existing support for source locations when emitting code from the IR A nice cleanup to this work would be to have the source locations for IR values not be hyper-specific about both line and column; it is probably enough to just emit on the correct line. --- source/slang/ir.cpp | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'source/slang/ir.cpp') diff --git a/source/slang/ir.cpp b/source/slang/ir.cpp index d4d2f0f51..38a0f0c03 100644 --- a/source/slang/ir.cpp +++ b/source/slang/ir.cpp @@ -244,6 +244,32 @@ namespace Slang addInst(parent, inst); } + static void maybeSetSourceLoc( + IRBuilder* builder, + IRValue* value) + { + if(!builder) + return; + + auto sourceLocInfo = builder->sourceLocInfo; + if(!sourceLocInfo) + return; + + // Try to find something with usable location info + for(;;) + { + if(sourceLocInfo->sourceLoc.getRaw()) + break; + + if(!sourceLocInfo->next) + break; + + sourceLocInfo = sourceLocInfo->next; + } + + value->sourceLoc = sourceLocInfo->sourceLoc; + } + static IRValue* createValueImpl( IRBuilder* /*builder*/, UInt size, @@ -273,7 +299,7 @@ namespace Slang // arguments *after* the type (which is a mandatory // argument for all instructions). static IRInst* createInstImpl( - IRBuilder* /*builder*/, + IRBuilder* builder, UInt size, IROp op, IRType* type, @@ -291,6 +317,8 @@ namespace Slang inst->type = type; + maybeSetSourceLoc(builder, inst); + auto operand = inst->getArgs(); for( UInt aa = 0; aa < fixedArgCount; ++aa ) @@ -858,6 +886,7 @@ namespace Slang this, kIROp_Func, nullptr); + maybeSetSourceLoc(this, rsFunc); addGlobalValue(getModule(), rsFunc); return rsFunc; } @@ -870,6 +899,7 @@ namespace Slang this, kIROp_global_var, ptrType); + maybeSetSourceLoc(this, globalVar); addGlobalValue(getModule(), globalVar); return globalVar; } @@ -3112,7 +3142,8 @@ namespace Slang } } - // TODO: implement this + // We will also clone the location here, just because this is a convenient bottleneck + clonedValue->sourceLoc = originalValue->sourceLoc; } struct IRSpecContext : IRSpecContextBase -- cgit v1.2.3