From af84d85799758234110fc42f0ba5c771dacb5fe3 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 7 Feb 2020 08:45:32 -0800 Subject: Change handling of strings for HLSL/GLSL targets (#1204) * Change handling of strings for HLSL/GLSL targets This change switches our handling of string literals and `getStringHash` to something that is more streamlined at the cost of potentially being less general/flexible. * `String` is now allowed as a parameter type in user-defined functions * `getStringHash` is now allowed to apply to `String`-type values that aren't literals * The list of strings in an IR module is now generated during IR lowering as part of lowering a string literal expression, rather than being defined by recursively walking the IR of the module looking for `getStringHash` calls. The public API still refers to these as "hashed" strings, but they are realistically now "static strings." * When emitting code for HLSL/GLSL, the `String` type emits as `int`, and `getStringHash(x)` emits as `x`. In terms of implementation, the choice was whether to translate `String` over to `int` in an explicit IR pass, or to lump it into the emit pass. While adding the logic to emit clutters up an already complicated step, it is ultimately much easier to make the change there than to write a clean IR pass to eliminate all `String` use. Note that other targets that can handle a more full-featured `String` type are *not* addressed by this change and thus do not support `String` at all. It may be woth emitting `String` as `const char*` on those targets, and emitting string literals directly, but the `getStringHash` function would need to be implemented in the "prelude" then, and we probably want to pick a well-known/-documented hash algorithm before we go that far. This change also brings along some some clean-ups to the `gpu-printing` example, since it can now take advantage of the new functionality of `String`. * Fix up tests for new string handling * Add global string literal list to string-literal test (since we now list *all* static string literals and not just those passed to `getStringHash`) * Disable `getStringHash` test on CPU, since we don't have a working `String` on that platform right now (only HLSL/GLSL) Co-authored-by: Tim Foley --- examples/gpu-printing/gpu-printing.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'examples/gpu-printing/gpu-printing.cpp') diff --git a/examples/gpu-printing/gpu-printing.cpp b/examples/gpu-printing/gpu-printing.cpp index ff35fd0b3..7503c8e03 100644 --- a/examples/gpu-printing/gpu-printing.cpp +++ b/examples/gpu-printing/gpu-printing.cpp @@ -219,7 +219,7 @@ void GPUPrinting::processGPUPrintCommands(const void* data, size_t dataSize) // likely that the application code needs to be configured // to pass in the right strings. // - fprintf(stderr, "error: string with unknown hash %d\n", hash); + fprintf(stderr, "error: string with unknown hash 0x%x\n", hash); continue; } @@ -253,7 +253,7 @@ void GPUPrinting::processGPUPrintCommands(const void* data, size_t dataSize) // likely that the application code needs to be configured // to pass in the right strings. // - fprintf(stderr, "error: string with unknown hash %d\n", formatHash); + fprintf(stderr, "error: string with unknown hash 0x%x\n", formatHash); continue; } std::string format = iter->second; @@ -373,7 +373,7 @@ void GPUPrinting::processGPUPrintCommands(const void* data, size_t dataSize) auto iter = m_hashedStrings.find(hash); if(iter == m_hashedStrings.end()) { - fprintf(stderr, "error: string with unknown hash %d\n", hash); + fprintf(stderr, "error: string with unknown hash 0x%x\n", hash); continue; } printf("%s", iter->second.c_str()); -- cgit v1.2.3