From 6660627558eb8ef9b6380b84532f726cd5d094c6 Mon Sep 17 00:00:00 2001 From: Graham Rushton Date: Tue, 20 Apr 2021 14:08:29 +0100 Subject: [PATCH] Fix concat of long hashed names in traces etc (#2854) --- docs/CONTRIBUTORS | 1 + src/V3String.cpp | 52 ++++++++++++++++--- test_regress/t/t_trace_complex.out | 4 ++ test_regress/t/t_trace_complex.v | 2 + test_regress/t/t_trace_complex_fst.out | 11 ++-- test_regress/t/t_trace_complex_fst_sc.out | 13 +++-- test_regress/t/t_trace_complex_params.out | 4 ++ test_regress/t/t_trace_complex_params_fst.out | 11 ++-- .../t/t_trace_complex_params_fst_sc.out | 13 +++-- 9 files changed, 87 insertions(+), 24 deletions(-) diff --git a/docs/CONTRIBUTORS b/docs/CONTRIBUTORS index e20735dd4..f8c20a846 100644 --- a/docs/CONTRIBUTORS +++ b/docs/CONTRIBUTORS @@ -23,6 +23,7 @@ Garrett Smith Geza Lore Gianfranco Costamagna Glen Gibb +Graham Rushton Harald Heckmann Howard Su Huang Rui diff --git a/src/V3String.cpp b/src/V3String.cpp index 5e4bcada6..b3d6a153f 100644 --- a/src/V3String.cpp +++ b/src/V3String.cpp @@ -380,12 +380,52 @@ void VHashSha256::selfTest() { // VName string VName::dehash(const string& in) { - const string::size_type pos = in.find("__Vhsh"); - if (VL_LIKELY(pos == string::npos)) return in; - const string vhsh = in.substr(pos); - const auto& it = s_dehashMap.find(vhsh); - UASSERT(it != s_dehashMap.end(), "String not in reverse hash map '" << vhsh << "'"); - return in.substr(0, pos) + it->second; + static const char VHSH[] = "__Vhsh"; + static const size_t DOT_LEN = strlen("__DOT__"); + std::string dehashed; + + // Need to split 'in' into components separated by __DOT__, 'last_dot_pos' + // keeps track of the position after the most recently found instance of __DOT__ + for (string::size_type last_dot_pos = 0; last_dot_pos < in.size(); ) { + const string::size_type next_dot_pos = in.find("__DOT__", last_dot_pos); + // Two iterators defining the range between the last and next dots. + auto search_begin = std::begin(in) + last_dot_pos; + auto search_end = next_dot_pos == string::npos ? std::end(in) + : std::begin(in) + next_dot_pos; + + // Search for __Vhsh between the two dots. + auto begin_vhsh = std::search(search_begin, search_end, + std::begin(VHSH), std::end(VHSH) - 1); + if (begin_vhsh != search_end) { + std::string vhsh(begin_vhsh, search_end); + const auto& it = s_dehashMap.find(vhsh); + UASSERT(it != s_dehashMap.end(), "String not in reverse hash map '" << vhsh << "'"); + // Is this not the first component, but the first to require dehashing? + if (last_dot_pos > 0 && dehashed.empty()) { + // Seed 'dehashed' with the previously processed components. + dehashed = in.substr(0, last_dot_pos); + } + // Append the unhashed part of the component. + dehashed += std::string(search_begin, begin_vhsh); + // Append the bit that was lost to truncation but retrieved from the dehash map. + dehashed += it->second; + } + // This component doesn't need dehashing but a previous one might have. + else if (!dehashed.empty()) { + dehashed += std::string(search_begin, search_end); + } + + if (next_dot_pos != string::npos) { + // Is there a __DOT__ to add to the dehashed version of 'in'? + if (!dehashed.empty()) { + dehashed += "__DOT__"; + } + last_dot_pos = next_dot_pos + DOT_LEN; + } else { + last_dot_pos = string::npos; + } + } + return dehashed.empty() ? in : dehashed; } string VName::hashedName() { diff --git a/test_regress/t/t_trace_complex.out b/test_regress/t/t_trace_complex.out index e57a057af..74c7eb8c7 100644 --- a/test_regress/t/t_trace_complex.out +++ b/test_regress/t/t_trace_complex.out @@ -39,6 +39,9 @@ $timescale 1ps $end $var wire 2 % v_strp [1:0] $end $var wire 4 & v_strp_strp [3:0] $end $var wire 2 ' v_unip_strp [1:0] $end + $scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end + $var wire 32 J PARAM [31:0] $end + $upscope $end $scope module p2 $end $var wire 32 H PARAM [31:0] $end $upscope $end @@ -92,6 +95,7 @@ b00000000 F 0G b00000000000000000000000000000010 H b00000000000000000000000000000011 I +b00000000000000000000000000000100 J #10 b00000000000000000000000000000001 $ b11 % diff --git a/test_regress/t/t_trace_complex.v b/test_regress/t/t_trace_complex.v index 14489e9c5..baae75453 100644 --- a/test_regress/t/t_trace_complex.v +++ b/test_regress/t/t_trace_complex.v @@ -78,6 +78,8 @@ module t (clk); p #(.PARAM(2)) p2 (); p #(.PARAM(3)) p3 (); + p #(.PARAM(4)) a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed (); + always @ (posedge clk) begin cyc <= cyc + 1; v_strp <= ~v_strp; diff --git a/test_regress/t/t_trace_complex_fst.out b/test_regress/t/t_trace_complex_fst.out index e4f264a8e..d57c690c6 100644 --- a/test_regress/t/t_trace_complex_fst.out +++ b/test_regress/t/t_trace_complex_fst.out @@ -58,15 +58,19 @@ $upscope $end $scope module p3 $end $var parameter 32 B PARAM $end $upscope $end +$scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end +$var parameter 32 C PARAM $end +$upscope $end $upscope $end $scope module $unit $end -$var bit 1 C global_bit $end +$var bit 1 D global_bit $end $upscope $end $upscope $end $enddefinitions $end -#0 $dumpvars -1C +#0 +1D +b00000000000000000000000000000100 C b00000000000000000000000000000011 B b00000000000000000000000000000010 A b00000000000000000000000000000000 @ @@ -101,7 +105,6 @@ b0000 $ b00 # b00000000000000000000000000000000 " 0! -$end #10 1! b00000000000000000000000000000001 " diff --git a/test_regress/t/t_trace_complex_fst_sc.out b/test_regress/t/t_trace_complex_fst_sc.out index 85c002638..f3da1ace4 100644 --- a/test_regress/t/t_trace_complex_fst_sc.out +++ b/test_regress/t/t_trace_complex_fst_sc.out @@ -1,5 +1,5 @@ $date - Thu Apr 1 15:12:03 2021 + Mon Apr 19 17:05:53 2021 $end $version @@ -57,15 +57,19 @@ $upscope $end $scope module p3 $end $var parameter 32 B PARAM $end $upscope $end +$scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end +$var parameter 32 C PARAM $end +$upscope $end $upscope $end $scope module $unit $end -$var bit 1 C global_bit $end +$var bit 1 D global_bit $end $upscope $end $upscope $end $enddefinitions $end -#0 $dumpvars -1C +#0 +1D +b00000000000000000000000000000100 C b00000000000000000000000000000011 B b00000000000000000000000000000010 A b00000000000000000000000000000000 @ @@ -100,7 +104,6 @@ b0000 $ b00 # b00000000000000000000000000000000 " 0! -$end #10 1! b00000000000000000000000000000001 " diff --git a/test_regress/t/t_trace_complex_params.out b/test_regress/t/t_trace_complex_params.out index 88690fd62..8d2cd1890 100644 --- a/test_regress/t/t_trace_complex_params.out +++ b/test_regress/t/t_trace_complex_params.out @@ -39,6 +39,9 @@ $timescale 1ps $end $var wire 2 % v_strp [1:0] $end $var wire 4 & v_strp_strp [3:0] $end $var wire 2 ' v_unip_strp [1:0] $end + $scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end + $var wire 32 J PARAM [31:0] $end + $upscope $end $scope module p2 $end $var wire 32 H PARAM [31:0] $end $upscope $end @@ -92,6 +95,7 @@ b00000000 F 0G b00000000000000000000000000000010 H b00000000000000000000000000000011 I +b00000000000000000000000000000100 J #10 b00000000000000000000000000000001 $ b11 % diff --git a/test_regress/t/t_trace_complex_params_fst.out b/test_regress/t/t_trace_complex_params_fst.out index e4f264a8e..d57c690c6 100644 --- a/test_regress/t/t_trace_complex_params_fst.out +++ b/test_regress/t/t_trace_complex_params_fst.out @@ -58,15 +58,19 @@ $upscope $end $scope module p3 $end $var parameter 32 B PARAM $end $upscope $end +$scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end +$var parameter 32 C PARAM $end +$upscope $end $upscope $end $scope module $unit $end -$var bit 1 C global_bit $end +$var bit 1 D global_bit $end $upscope $end $upscope $end $enddefinitions $end -#0 $dumpvars -1C +#0 +1D +b00000000000000000000000000000100 C b00000000000000000000000000000011 B b00000000000000000000000000000010 A b00000000000000000000000000000000 @ @@ -101,7 +105,6 @@ b0000 $ b00 # b00000000000000000000000000000000 " 0! -$end #10 1! b00000000000000000000000000000001 " diff --git a/test_regress/t/t_trace_complex_params_fst_sc.out b/test_regress/t/t_trace_complex_params_fst_sc.out index dcfa501c1..b5e67d184 100644 --- a/test_regress/t/t_trace_complex_params_fst_sc.out +++ b/test_regress/t/t_trace_complex_params_fst_sc.out @@ -1,5 +1,5 @@ $date - Thu Apr 1 15:17:39 2021 + Mon Apr 19 17:07:10 2021 $end $version @@ -57,15 +57,19 @@ $upscope $end $scope module p3 $end $var parameter 32 B PARAM $end $upscope $end +$scope module a_module_instantiation_with_a_very_long_name_that_once_its_signals_get_concatenated_and_inlined_will_almost_certainly_result_in_them_getting_hashed $end +$var parameter 32 C PARAM $end +$upscope $end $upscope $end $scope module $unit $end -$var bit 1 C global_bit $end +$var bit 1 D global_bit $end $upscope $end $upscope $end $enddefinitions $end -#0 $dumpvars -1C +#0 +1D +b00000000000000000000000000000100 C b00000000000000000000000000000011 B b00000000000000000000000000000010 A b00000000000000000000000000000000 @ @@ -100,7 +104,6 @@ b0000 $ b00 # b00000000000000000000000000000000 " 0! -$end #10 1! b00000000000000000000000000000001 "