Modelsim Optimization Issue - optimization

I am having problem when I am trying to run the following verilog code snippet in Optimized mode using Modelsim simulator v10.2c.
always # *
if (dut.rtl_module.enable == 1'b1)
force dut.rtl_module.abc_reg = xyz;
If the above snippet is run in non-optimized mode, this works fine. But for optimized mode, it fails.
PS: I am using -O5 optimization level

Optimisation typically disables access to simulator objects. Your force command requires that access.
You'll need to explicitly enable access. Unfortunately I can't see anything useful in the Modelsim AE documentation, however from Riviera-PRO:
+accs
Enables access to design structure. This is the default in -O0,
-O1 and -O2 and may optionally be specified for -O3. If omitted,
the compiler may limit read access to selected objects.
Modelsim supports +acc, it just doesn't appear to be well documented. The only reference appears to be this suggestion:
While optimization is not necessary for class based debugging, you might want to use
vsim -voptargs=+acc=lprn to enable visibility into your design for RTL debugging.

Related

run LLVM opt hotcoldsplit issue

Hot cold splitting is an effective way for code optimization in LLVM.
This built-in LLVM pass is located at :
/llvm/lib/Transforms/IPO/HotColdSplitting.cpp
Actually, I want to use this pass to optimize my code but I didn't find any documentation on how to use this built-in pass to optimize my code .
I already know that I should use LLVM opt command to load the pass but I didn't find the proper way to apply this optimization pass on my program .
I have two questions so far :
1) How to use opt properly to load this pass to optimize my code
2) Can I use this pass directly on clang to optimize C/C++ code as switches like -fsanitize=address which applies to the underlying compiling program ?
Thanks.
You can pass the -mllvm -hot-cold-split=true flag to clang, which will enable hot/cold splitting pass in the optimizer when compiling your file.
Yes, in principle you can directly use this pass (as of the time of answering the question); hot/cold splitting in LLVM, in its current form, only optimizes for code size. Alternatively you might want to try first collecting profiling data via PGO, and then feeding the profiling data into clang for it to take advantage of profile information during the build (which might help hot/cold splitting in terms of performance).
Hot cold splitting can be used to optimize an app for startup performance, as well as for runtime performance in some cases. To enable hot cold splitting optimization you can pass the flag to llvm using -mllvm -hot-cold-split.
Hot cold splitting gives best performance improvement in the presence of profile data. Although it does optimize applications without profile data using inbuilt static analysis. For example: catch block, non returning functions are already known to be cold. Hot cold splitting uses these information.
Currently there is no direct flag from the clang frontend to enable this so you'll have to use -mllvm -hot-cold-split. For more details on hot cold splitting the youtube video at the llvm-dev is quite informative: https://www.youtube.com/watch?v=Q8rqGg6vHAE

Howto enable stronger optimization builds

I am trying to build PETSc and have problems to enable optimization. Without specifying, PETSc always creates a debugging build, but I can turn that off with passing --with-debugging=0 to cmake. However, this only enables -O1 by default, but as my application is extreme time consuming and very time critical, I want to have at least -O2. I can't find an option except --CFLAGS, which works, but always appends options to the end, so -O1 would override my -O2.
I greped for "-O" to set the flag manually, this gave me a million lines, mostly from the configure.log file and doesn't help.
Does anybody know the file where to set the flag, or a workaround like ...another option that disables the usage of the last specified -O#, but enables the strongest or first?
Citing PETSc' install instructions:
Configure defaults to building PETSc in debug mode. One can switch to
using optimzed mode with the toggle option --with-debugging [defaults
to debug enabled]. Additionally one can specify more suitable
optimization flags with the options COPTFLAGS, FOPTFLAGS, CXXOPTFLAGS.
./configure --with-cc=gcc --with-fc=gfortran --with-debugging=0
COPTFLAGS='-O3 -march=p4 -mtune=p4' FOPTFLAGS='-O3 -qarch=p4
-qtune=p4'

How to enable the ARC optimizer in debug configuration?

The Transitioning to ARC Release Notes makes this statement:
One issue to be aware of is that the optimizer is not run in common
debug configurations, so expect to see a lot more retain/release
traffic at -O0 than at -Os.
How can we enable the optimizer in a default debug configuration?
You can set the optimization level in Xcode's Build Settings independently of for the Debug and Release configurations - just go to build settings, scroll down till you find the optimization setting, and pick the one you want from the menu.
Note: You should probably only do this for curiosity (which is to be encouraged :-)), as optimization can (re)move code etc. debugging may become a little harder, e.g. a variable may "disappear" so you can't so easily track its value as its been assigned to a register.

why is my code performing poorly when built with Realview tools but better with Codesourcery?

I have a C project which was previously being built with Codesourcery's gnu tool chain. Recently it was converted to use Realview's armcc compiler but the performance that we are getting with Realview tools is very poor compared to when it is compiled with gnu tools. Shouldnt it be opposite case i.e it should give better performance when compiled with Realview's tools? What am I missing here. How can I improve the performance with Realview's tools?
Also I have noticed that if I run the binary produced by Realview Tools with Lauterbach it crashes but If I run it using Realview ICE it runs fine.
UPDATE 1
Realview Command line:
armcc -c --diag_style=ide
--depend_format=unix_escaped --no_depend_system_headers --no_unaligned_access --c99 --arm_only --debug --gnu --cpu=ARM1136J-S --fpu=SoftVFP --apcs=/nointerwork -O3 -Otime
GNU GCC command line:
arm-none-eabi-gcc -mcpu=arm1136jf-s
-mlittle-endian -msoft-float -O3 -Wall
I am using Realview Tools version 4.1 and GCC version 4.4.1
UPDATE 2
Lauterbach issue has been solved. It was being caused because of Semihosting as the semihosting SWI was not being handled in Lauterbach environment. Retargeting the C library to avoid Semihosting did the trick and now my program runs successfully with Lauterbach as well as Realview ICE. But the performance issue is as it is.
Since you have optimisations on, and in some environments it crashes, it may be that your code uses undefined behaviour or other latent error. Such behaviour can change with optimisation, or even break altogether.
I suggest that you try both tool-chains without optimisation, and make sure that the warning level is set high, and you fix them all. GCC is far better that armcc at error checking so is a reasonable static analysis check. If the code builds clean it is more likely to work and may be easier for the optimiser to handle.
Have you tried removing the '--no_unaligned_access'? ARM11s can typically do unaligned access (if enabled in the startup code) and forcing the compiler/library to not do them may be slowing down your code.
The current version of RVCT says of '--fpu=SoftVFP':
In previous releases of RVCT, if you
specified --fpu=softvfp and a CPU with
implicit VFP hardware, the linker
chose a library that implemented the
software floating-point calls using
VFP instructions. This is no longer
the case. If you require this legacy
behavior, use --fpu=softvfp+vfp.
This suggests to me that if you perhaps have an old version of RVCT the behaviour will be to use software floating point regardless of the presence of hardware floating point. While in the GNU version -msoft-float will use hardware floating point instructions when an FPU is available.
So what version of RVCT are you using?
Either way I suggest that you remove the --fpu option since the compiler will make an implicit appropriate selection based on the --cpu option selected. You also need to correct the CPU selection, your RVCT option says --cpu=ARM1136J-S not ARM1136FJ-S as you told GCC. This will no doubt prevent the compiler from generating VFP instructions, since you told it it has no VFP.
The same source code can produce dramatically different binaries due to factors like. Different compilers (llvm vs gcc, gcc 4 vs gcc3, etc). Different versions of the same compiler. Different compiler options if the same compiler. Optimization (on either compiler). Compiled for release or debug (or whatever terms you want to use, the binaries are quite different). When going embedded, you add in the complication of a bootloader or rom monitor (debugger) and things like that. Then add to that the host side tools that talk to the rom monitor or compiled in debugger. Despite being a far better compiler than gcc, arm compilers were infected with the assumption that the binaries would always be run on top of their rom monitor. I want to remember that by the time rvct became their primary compiler that assumption was on its way out, but I have not really used their tools since then.
The bottom line is there are a handful of major factors that can affect the differences between binaries that can and will lead to a different experience. Assuming that you will get the same performance or results, is a bad assumption, the expectation is that the results will differ. Likewise, within the same environment, you should be able to create binaries that give dramatically different performance results. All from the same source code.
Do you have compiler optimizations turned on in your CodeSourcery build, but not in the Realview build?

Any Macro or Technic for Part Optimization?

I am working on lock free structure with g++ compiler. It seems that with -o1 switch, g++ will change the execution order of my code. How can I forbid g++'s optimization on certain part of my code while maintain the optimization to other part? I know I can split it to two files and link them, but it looks ugly.
If you find that gcc changes the order of execution in your code, you should consider using a memory barrier. Just don't assume that volatile variables will protect you from that issue. They will only make sure that in a single thread, the behavior is what the language guarantees, and will always read variables from their memory location to account for changes "invisible" to the executing code. (e.g changes to a variable done by a signal handler).
GCC supports OpenMP since version 4.2. You can use it to create a memory barrier with a special #pragma directive.
A very good insight about locking free code is this PDF by Herb Sutter and Andrei Alexandrescu: C++ and the Perils of Double-Checked Locking
You can use a function attribute "__attribute__ ((optimize 0))" to set the optimization for a single function, or "#pragma GCC optimize" for a block of code. These are only for GCC 4.4, though, I think - check your GCC manual. If they aren't supported, separation of the source is your only option.
I would also say, though, that if your code fails with optimization turned on, it is most likely that your code is just wrong, especially as you're trying to do something that is fundamentally very difficult. The processor will potentially perform reordering on your code (within the limits of sequential consistency) so any re-ordering that you're getting with GCC could potentially occur anyway.