Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign up`cfg_target_feature` and `target_feature` don't interact properly #42515
Comments
This comment has been minimized.
This comment has been minimized.
|
It does panic. #![feature(cfg_target_feature, target_feature)]
#[target_feature = "+avx"]
fn should_panic() {
#[cfg(target_feature = "avx")]
panic!("have_avx");
}
fn main() {
should_panic();
}
If you don't compile with the appropriate target-feature settings, then the |
This comment has been minimized.
This comment has been minimized.
|
But |
This comment has been minimized.
This comment has been minimized.
|
What do gcc and clang do? Can you explain more about your use case and why you expect this to happen? |
This comment has been minimized.
This comment has been minimized.
|
Well gcc and c only have the preprocessor macros so that would behave like cfg_target_feature currently does. However, I assumed rust attributes are more context aware. |
This comment has been minimized.
This comment has been minimized.
|
My use case is for this procedural macro attribute I've written https://github.com/parched/runtime-target-feature-rs which generates 2 copies of the function, one with |
This comment has been minimized.
This comment has been minimized.
|
@parched gcc and Clang have the equivalent of
To give you some context, the
But |
This comment has been minimized.
This comment has been minimized.
|
More explicitly the macro I've written,
|
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi I mean they only have macros for detecting the target features so the couldn't possibly change inside a function with |
This comment has been minimized.
This comment has been minimized.
|
@parched Sorry, but I still don't understand your use case. What problem are you trying to solve at a high level? For example, having a |
This comment has been minimized.
This comment has been minimized.
|
And I still don't understand what's fundamentally different between Rust's system and gcc's/Clang's. I think you need to back way up and help me try to understand what specific problems you're facing. |
This comment has been minimized.
This comment has been minimized.
|
@BurntSushi: I think the use case here is a matter of convenience: Write a function in which some parts are marked The difference to gcc/Clang is, as I understand it, that since the preprocessor runs before any c parsing happens it cannot know if it is inside a function marked |
This comment has been minimized.
This comment has been minimized.
|
@TimNN Thanks for explaining that. It makes a little more sense, although I don't understand why the macro can't just strip out the cfg'd code that's never going to execute. Popping up a level, I'm not particularly familiar with this aspect of the compiler (can arbitrary attributes impact the resolution of |
This comment has been minimized.
This comment has been minimized.
|
Yes just what TimNN says. @BurntSushi I was actually just thinking that on my bike ride home and that would definitely work. It would mean the macro would need to know all the target features and their hierarchy. I.e., if the feature enabled was sse3 the macro would have to know to strip out any cfg not sse2. |
Mark-Simulacrum
added
the
T-lang
label
Jun 23, 2017
parched
referenced this issue
Jun 25, 2017
Open
Have a way to write different code based on the runtime detected features #13
Mark-Simulacrum
added
the
C-bug
label
Jul 27, 2017
This comment has been minimized.
This comment has been minimized.
|
Does the following help? #[inline(always)]
fn foo_impl() {
// ...generic implementation here...
#[cfg(traget_feature = "+avx")] {
// some avx specific optimizaiton here
}
#[cfg(not(target_feature = "+avx"))] {
// some non-avx generic alternative here
}
// ... more generic implementation ...
}
#[target_feature = "+avx"] fn foo_avx() { foo_impl() }
#[target_feature = "+sse3"] fn foo_sse3() { foo_impl() }That is, could we propagate the target features of The trivial case of this is what @parched proposed. |
alexcrichton
added
the
A-simd
label
Sep 25, 2017
gnzlbg
referenced this issue
Sep 26, 2017
Open
Tracking issue for RFC 2045: improving `#[target_feature]` #44839
parched
referenced this issue
Dec 21, 2017
Open
macro does not work on arrays, no way to have a manual default implementation #18
This comment has been minimized.
This comment has been minimized.
|
Propagating through function calls as @gnzlbg described is entirely incompatible with how Edit: Sorry, the following doesn't make sense
|
This comment has been minimized.
This comment has been minimized.
Yes, but IMO worth it. An alternative would be to make |
parched commentedJun 7, 2017
•
edited
This should panic but it doesn`t
I would like for this to work because I have a macro that generates 2 copys of a function, one with
#[target_feature = "+feat"]and one without and I want to conditionally use some assembly when the feature is available.CC #29717