gem5-dev@gem5.org

The gem5 Developer List

View all threads

[M] Change in gem5/gem5[develop]: mem-ruby: Optimize MachineType_base_number calculation

GB
Gabriel B. (Gerrit)
Mon, Feb 6, 2023 5:38 PM

Gabriel B. has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/67674?usp=email )

Change subject: mem-ruby: Optimize MachineType_base_number calculation
......................................................................

mem-ruby: Optimize MachineType_base_number calculation

Values returned by MachineType_base_number can be computed once and
for all after ellaboration. Thus, they are stored in local static
variables and the initialization logic is guarded by a once-flag. The
function is now a simple switch during subsequent calls.

Change-Id: If93628851322566e58015c16267093640f526926

M src/mem/slicc/symbols/Type.py
1 file changed, 47 insertions(+), 26 deletions(-)

diff --git a/src/mem/slicc/symbols/Type.py b/src/mem/slicc/symbols/Type.py
index a8735a7..a0bcd7b 100644
--- a/src/mem/slicc/symbols/Type.py
+++ b/src/mem/slicc/symbols/Type.py
@@ -863,35 +863,42 @@
*/
int
${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj)
-{

  • int base = 0;
  • switch(obj) {
    -"""
  •        )
    
  •        # For each field
    

+{""")

  •        ev = list(self.enums.values())
            code.indent()
    
  •        code("  case ${{self.c_ident}}_NUM:")
    
  •        for enum in reversed(list(self.enums.values())):
    
  •            # Check if there is a defined machine with this type
    
  •            if enum.primary:
    
  •                code(
    
  •                    "    base +=  
    

${{enum.ident}}_Controller::getNumControllers();"

  •                )
    
  •            else:
    
  •                code("    base += 0;")
    
  •            code("    [[fallthrough]];")
    
  •            code("  case ${{self.c_ident}}_${{enum.ident}}:")
    
  •        code("    break;")
    
  •        # Caching hack. Controller instance counts are global and  
    

constant

  •        # anyway so why not abusing that by caching all values here ?
    
  •        # Caching is usefull as this function is called frequently  
    

during a

  •        # simulation (especially for MachineType).
    
  •        code("static bool initialized{false};")
    
  •        for e in ev:
    
  •            code("static int ${{self.c_ident}}_${{e.ident}}_base;")
    
  •        code("if (GEM5_UNLIKELY(!initialized)) {")
    
  •        code.indent()
    
  •        code("initialized = true;")
    
  •        for e0, e1 in zip(ev[:-1], ev[1:]):
    
  •            code("${{self.c_ident}}_${{e1.ident}}_base = ")
    
  •            code.indent()
    
  •            code("${{self.c_ident}}_${{e0.ident}}_base +")
    
  •            code("AbstractController::controllerCount("
    
  •                    "${{self.c_ident}}_${{e0.ident}});")
    
  •            code.dedent()
            code.dedent()
    
  •        code("}")
    
  •        code(
    
  •            """
    
  •  default:
    
  •    panic("Invalid range for type ${{self.c_ident}}");
    
  • }
  • return base;
  •        code("switch(obj) {")
    
  •        code.indent()
    
  •        for enum in self.enums.values():
    
  •            code("case ${{self.c_ident}}_${{enum.ident}}: "
    
  •                    "  return ${{self.c_ident}}_${{enum.ident}}_base;")
    
  •        code("case ${{self.c_ident}}_NUM: "
    
  •             "return ${{self.c_ident}}_${{ev[-1].ident}}_base;")
    
  •        code('default: panic("Invalid range for type  
    

${{self.c_ident}}:"'

  •             '" %d", int(obj));')
    
  •        code.dedent()
    
  •        code("}")
    
  •        code.dedent()
    
  •        code("""
    

    }

    /** \brief returns the total number of components for each machine

--
To view, visit
https://gem5-review.googlesource.com/c/public/gem5/+/67674?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: If93628851322566e58015c16267093640f526926
Gerrit-Change-Number: 67674
Gerrit-PatchSet: 1
Gerrit-Owner: Gabriel B. gabriel.busnot@arteris.com
Gerrit-MessageType: newchange

Gabriel B. has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/67674?usp=email ) Change subject: mem-ruby: Optimize MachineType_base_number calculation ...................................................................... mem-ruby: Optimize MachineType_base_number calculation Values returned by MachineType_base_number can be computed once and for all after ellaboration. Thus, they are stored in local static variables and the initialization logic is guarded by a once-flag. The function is now a simple switch during subsequent calls. Change-Id: If93628851322566e58015c16267093640f526926 --- M src/mem/slicc/symbols/Type.py 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/src/mem/slicc/symbols/Type.py b/src/mem/slicc/symbols/Type.py index a8735a7..a0bcd7b 100644 --- a/src/mem/slicc/symbols/Type.py +++ b/src/mem/slicc/symbols/Type.py @@ -863,35 +863,42 @@ */ int ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) -{ - int base = 0; - switch(obj) { -""" - ) - - # For each field +{""") + ev = list(self.enums.values()) code.indent() - code(" case ${{self.c_ident}}_NUM:") - for enum in reversed(list(self.enums.values())): - # Check if there is a defined machine with this type - if enum.primary: - code( - " base += ${{enum.ident}}_Controller::getNumControllers();" - ) - else: - code(" base += 0;") - code(" [[fallthrough]];") - code(" case ${{self.c_ident}}_${{enum.ident}}:") - code(" break;") + # Caching hack. Controller instance counts are global and constant + # anyway so why not abusing that by caching all values here ? + # Caching is usefull as this function is called frequently during a + # simulation (especially for MachineType). + code("static bool initialized{false};") + for e in ev: + code("static int ${{self.c_ident}}_${{e.ident}}_base;") + code("if (GEM5_UNLIKELY(!initialized)) {") + code.indent() + code("initialized = true;") + for e0, e1 in zip(ev[:-1], ev[1:]): + code("${{self.c_ident}}_${{e1.ident}}_base = ") + code.indent() + code("${{self.c_ident}}_${{e0.ident}}_base +") + code("AbstractController::controllerCount(" + "${{self.c_ident}}_${{e0.ident}});") + code.dedent() code.dedent() + code("}") - code( - """ - default: - panic("Invalid range for type ${{self.c_ident}}"); - } - - return base; + code("switch(obj) {") + code.indent() + for enum in self.enums.values(): + code("case ${{self.c_ident}}_${{enum.ident}}: " + " return ${{self.c_ident}}_${{enum.ident}}_base;") + code("case ${{self.c_ident}}_NUM: " + "return ${{self.c_ident}}_${{ev[-1].ident}}_base;") + code('default: panic("Invalid range for type ${{self.c_ident}}:"' + '" %d", int(obj));') + code.dedent() + code("}") + code.dedent() + code(""" } /** \\brief returns the total number of components for each machine -- To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/67674?usp=email To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings Gerrit-Project: public/gem5 Gerrit-Branch: develop Gerrit-Change-Id: If93628851322566e58015c16267093640f526926 Gerrit-Change-Number: 67674 Gerrit-PatchSet: 1 Gerrit-Owner: Gabriel B. <gabriel.busnot@arteris.com> Gerrit-MessageType: newchange