From 25f1b2dd84c51ba52ccb35e80da0aa71d200a1b8 Mon Sep 17 00:00:00 2001 From: jithunnair-amd Date: Tue, 10 Jul 2018 15:53:53 -0500 Subject: [PATCH 1/4] Add workarounds in pyHIPIFY for __forceinline__ and std:: math functions in HIP --- tools/amd_build/pyHIPIFY/hipify-python.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tools/amd_build/pyHIPIFY/hipify-python.py b/tools/amd_build/pyHIPIFY/hipify-python.py index bd38e26949492..2ed98806228ba 100755 --- a/tools/amd_build/pyHIPIFY/hipify-python.py +++ b/tools/amd_build/pyHIPIFY/hipify-python.py @@ -331,6 +331,18 @@ def disable_asserts(input_string): output_string = output_string.replace(input_string[start:p_end + 1], "") return output_string +def replace_forceinline(input_string): + output_string = input_string + output_string = re.sub("__forceinline__", "inline", output_string) + return output_string + +def replace_math_functions(input_string): + """ Replace std:: invocations of match functions with non-std:: versions""" + output_string = input_string + output_string = re.sub("std::exp\(", "::exp(", output_string) + output_string = re.sub("std::log\(", "::log(", output_string) + output_string = re.sub("std::pow\(", "::pow(", output_string) + return output_string def disable_function(input_string, function, replace_style): """ Finds and disables a function in a particular file. @@ -497,6 +509,12 @@ def preprocessor(filepath, stats): if not filepath.endswith("THCGeneral.h.in"): output_source = disable_asserts(output_source) + # Replace std:: with non-std:: versions + output_source = replace_math_functions(output_source) + + # Replace __forceinline__ with inline + output_source = replace_forceinline(output_source) + # Overwrite file contents fileobj.seek(0) fileobj.write(output_source) From 759417ed09b2d284283a5c29daabc5b32eed85c8 Mon Sep 17 00:00:00 2001 From: jithunnair-amd Date: Tue, 10 Jul 2018 18:43:15 -0500 Subject: [PATCH 2/4] Adding doc string to function --- tools/amd_build/pyHIPIFY/hipify-python.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/amd_build/pyHIPIFY/hipify-python.py b/tools/amd_build/pyHIPIFY/hipify-python.py index 2ed98806228ba..f7ddfcf748c90 100755 --- a/tools/amd_build/pyHIPIFY/hipify-python.py +++ b/tools/amd_build/pyHIPIFY/hipify-python.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 """ The Python Hipify script. ## # Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved. @@ -332,12 +332,17 @@ def disable_asserts(input_string): return output_string def replace_forceinline(input_string): + """__forceinline__'d methods can cause 'symbol multiply defined' errors in HIP. + Adding 'static' to all such methods leads to compilation errors, so + replacing '__forceinline__' with 'inline' as a workaround + https://github.com/ROCm-Developer-Tools/HIP/blob/master/docs/markdown/hip_faq.md#what-if-hip-generates-error-of-symbol-multiply-defined-only-on-amd-machine + """ output_string = input_string output_string = re.sub("__forceinline__", "inline", output_string) return output_string def replace_math_functions(input_string): - """ Replace std:: invocations of match functions with non-std:: versions""" + """ Replace std:: invocations of match functions with non-std:: versions to prevent linker errors""" output_string = input_string output_string = re.sub("std::exp\(", "::exp(", output_string) output_string = re.sub("std::log\(", "::log(", output_string) From 9f85af0bd706aac9580ad16faf7963fa92b22f6c Mon Sep 17 00:00:00 2001 From: jithunnair-amd Date: Thu, 12 Jul 2018 13:53:39 -0500 Subject: [PATCH 3/4] Revert accidental python version change --- tools/amd_build/pyHIPIFY/hipify-python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/amd_build/pyHIPIFY/hipify-python.py b/tools/amd_build/pyHIPIFY/hipify-python.py index f7ddfcf748c90..171d63d6cad5f 100755 --- a/tools/amd_build/pyHIPIFY/hipify-python.py +++ b/tools/amd_build/pyHIPIFY/hipify-python.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/python """ The Python Hipify script. ## # Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved. From 31240b66ead2c2015b26c5fb8cb1262971fdbdcf Mon Sep 17 00:00:00 2001 From: jithunnair-amd Date: Thu, 12 Jul 2018 14:40:26 -0500 Subject: [PATCH 4/4] Add comments to mention correctness caveat for replace_math_functions function --- tools/amd_build/pyHIPIFY/hipify-python.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/amd_build/pyHIPIFY/hipify-python.py b/tools/amd_build/pyHIPIFY/hipify-python.py index 171d63d6cad5f..7aae2481099fd 100755 --- a/tools/amd_build/pyHIPIFY/hipify-python.py +++ b/tools/amd_build/pyHIPIFY/hipify-python.py @@ -342,7 +342,10 @@ def replace_forceinline(input_string): return output_string def replace_math_functions(input_string): - """ Replace std:: invocations of match functions with non-std:: versions to prevent linker errors""" + """ FIXME: Temporarily replace std:: invocations of math functions with non-std:: versions to prevent linker errors + NOTE: This can lead to correctness issues when running tests, since the correct version of the math function (exp/expf) might not get called. + Plan is to remove this function once HIP supports std:: math function calls inside device code + """ output_string = input_string output_string = re.sub("std::exp\(", "::exp(", output_string) output_string = re.sub("std::log\(", "::log(", output_string)