Altera Nios II C2H Compiler User Manual

Page 21

Advertising
background image

Altera Corporation

9.1

1–15

November 2009

Nios II C2H Compiler User Guide

Introduction to the C2H Compiler

If the code contains C syntax not supported by the C2H Compiler, it
cannot be accelerated. Examples are floating point operations and
recursive functions. Refer to

Chapter 7, ANSI C Compliance and

Restrictions

.

Code that calls system and runtime library functions is a poor
candidate for acceleration. For example, there is little point in
accelerating

printf()

or

malloc()

. The underlying code contains

a complex set of sequential operations and does not contain
performance-critical loops.

Code that makes extensive use of global or external variables is a
poor candidate for acceleration. Each time the C2H accelerator uses
a global or external variable, it must access the Nios II processor’s
data memory, which is likely to cause a bottleneck.

There are exceptions to these guidelines. For example:

Experienced C coders often "unroll" iterative algorithms,
representing them as a sequential set of operations to work better
with an optimizing C compiler. If you can refactor the code and "roll
up" the loop, you might be able to create an efficient hardware
accelerator.

A critical inner loop might have a complex set of sequential
operations which, if accelerated in hardware, consumes a lot of logic
resources. This presents a trade-off: If the processor spends an
unacceptable amount of time in this loop, it might be worth the
hardware cost to accelerate the whole loop.

Some runtime library functions are iterative in nature. Examples
include common data movement functions and buffer set functions,
such as

memcpy()

or

memset()

. If your code calls one of these

functions, you might consider writing a simple, custom
implementation of the function, which you can then accelerate.

If your code uses global or external variables, it might be easy for you
to refactor it to be suitable for acceleration. Refactor your code to
copy the global or external variables to local storage, perform the
calculation with the local variables, and then copy results back to
global or external storage. The C2H Compiler implements local
variables as fast, pipelined registers inside of the accelerator.

Understanding Code to Find Opportunities for Acceleration

The best way to obtain optimal results with the C2H Compiler is to
understand your code, and know where the critical loops are. If you
wrote the program from scratch, you probably understand where the
critical sections of code are. If you are starting with an existing code base
that you want to accelerate, the C2H Compiler can benefit you to the

Advertising