UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Voyager
Voyager
5,501 Views
Registered: ‎04-10-2012

difference between ifdef and if

Jump to solution

There are some blocks in my code (I am using verilog) that we do some if checks of a flag to determine which modules to instantiate.  The flag is set in the synthesis properties as a generic (something like DO_CHIPSCOPE="YES").  

 

What I assumed (go ahead, start your jokes) was that since DO_CHIPSCOPE is only set in the properties (and never changed in the code), that the tools would trip out the sections of code that would never happen.

 

What in practice seems to be happening, is that they are there, but never running.  Is that correct?

 

But, I think that if I make it an ifdef instead, it would not ever build it.  Is that right?  Why are they different?

0 Kudos
1 Solution

Accepted Solutions
Historian
Historian
6,779 Views
Registered: ‎02-25-2008

Re: difference between ifdef and if

Jump to solution

`ifdef is a compiler directive. If the argument is not defined, the compiler ignores everything between the `ifdef and the closing `endif.

 

if (something) is a conditional that is part of a continous assignment. Whether the compiler optimizes away everything in the block if the condition is always false is up to the compiler. My guess that it should, and you should do some simple tests to be sure.

----------------------------Yes, I do this for a living.
0 Kudos
5 Replies
Historian
Historian
5,478 Views
Registered: ‎02-25-2008

Re: difference between ifdef and if

Jump to solution

The specific difference is that `ifdef tests to see if the argument was defined; its actual value is not interesting. 

`if tests to see if the given argument is true or false.

----------------------------Yes, I do this for a living.
0 Kudos
Voyager
Voyager
5,476 Views
Registered: ‎04-10-2012

Re: difference between ifdef and if

Jump to solution

You know, I didn't even realize that there was a `if option.... Whoops.....

 

 I was speaking about a source code if statement, for instance.  Would the blink LED pseudo code basically be dropped from the design (take up no resources) due to the compiler being smart enough to know that they will never happen?

code 1:

`define FOO
`ifdef FOO//do nothing
`else
     blink LED;
`endif

code 2:

reg foo = 1'b0;
if(foo)
begin
    blink LED;
end

 

It seems like even though code 2 should never occur, things are being instantiated in the design.  I am just trying to get some clarification if the tools treat `ifdef and if (not `if) differently.

 

 

0 Kudos
Historian
Historian
6,780 Views
Registered: ‎02-25-2008

Re: difference between ifdef and if

Jump to solution

`ifdef is a compiler directive. If the argument is not defined, the compiler ignores everything between the `ifdef and the closing `endif.

 

if (something) is a conditional that is part of a continous assignment. Whether the compiler optimizes away everything in the block if the condition is always false is up to the compiler. My guess that it should, and you should do some simple tests to be sure.

----------------------------Yes, I do this for a living.
0 Kudos
Highlighted
Voyager
Voyager
5,466 Views
Registered: ‎04-10-2012

Re: difference between ifdef and if

Jump to solution

OK, I think that you hit on the key.  It should optimize it out, but just like with regular programming, it has to make that decision, and it may not.  For the case I was looking at, it didn't.  I am not sure why not, but it seems like the `ifdef approach works more consistently (with the downside of the statements being more difficult since they can't have values, but such is life).

 

Thanks.

0 Kudos
Historian
Historian
5,460 Views
Registered: ‎02-25-2008

Re: difference between ifdef and if

Jump to solution

@garengllc wrote:

OK, I think that you hit on the key.  It should optimize it out, but just like with regular programming, it has to make that decision, and it may not.  For the case I was looking at, it didn't.  I am not sure why not, but it seems like the `ifdef approach works more consistently (with the downside of the statements being more difficult since they can't have values, but such is life).

 

Thanks.


The difference is that `ifdef always does as you expect. If you want to include or exclude code, you should use it instead of setting a signal to a constant 0 and hoping that it works. The signal-as-constant is a hack, essentially.

 

Also, look into the generate statement, which was designed for exactly what you're trying to do.

----------------------------Yes, I do this for a living.
0 Kudos