Bad diagnostic when trying to use ifunc from C++
Trying to compile this sample ifunc program produces an unhelpful error:
int foo(int x, int y) {
return x + y + 1;
}
int bar(int x, int y) {
return x + y + 2;
}
extern bool use_bar;
static void (*resolver (void)) (void) {
return use_bar ? (void(*)()) use_bar : (void(*)())foo;
}
__attribute__((ifunc("resolver")))
int call_ifunc(int x, int y);
$ clang++ ifunc.cpp
ifunc.cpp:15:16: error: ifunc must point to a defined function
__attribute__((ifunc("resolver")))
^
1 error generated.
This is apparently due to this expecting the mangled name of the function. It compiles if I trade static for extern "C". It would be better if it noticed you were trying to call a function with mangling and inform how to fix it.
@llvm/issue-subscribers-good-first-issue
Hello,
I am interested in working on this issue. It seems that the place to fix is CodeGenModule::checkAliasedGlobal() (https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L351-L354).
However, I am not sure how to lookup a non-mangled name in CodeGenModule, as I cannot see a method for it. I imagine the fix is something like "lookup given name to find symbols -> see if their mangled name exists in the current TU -> provide fix as a note", but I am not sure how to implement it.
The error message "ifunc must point to a defined function" is indicating that the resolver function that is being passed to the attribute((ifunc("resolver"))) attribute is not a defined function. This means that resolver either has not been declared correctly, or has not been defined correctly in the code.
The code example is written in C++, but the ifunc attribute is typically used with C. In C++, function pointers cannot be cast from one type to another without explicit casting, but in C they can. The issue might be resolved by changing the type of resolver to int (*resolver)(int, int).
This error is occurring because the ifunc attribute is applied to a function pointer call_ifunc, but it points to a function resolver which returns a pointer to a function. The ifunc attribute is expected to point to a function that returns a pointer to a function. To resolve this error, modify the definition of resolver to return a function pointer:
`**code**
typedef int (*function_pointer)(int, int);
static function_pointer resolver (void) {
return use_bar ? bar : foo;
}
__attribute__((ifunc("resolver")))
int call_ifunc(int x, int ``y);]([url](url))
...
This error is occurring because the ifunc attribute is applied to a function pointer call_ifunc, but it points to a function resolver which returns a pointer to a function. The ifunc attribute is expected to point to a function that returns a pointer to a function. To resolve this error, modify the definition of resolver to return a function pointer:
...
Hmm, this code still seems to cause the issue for me (https://godbolt.org/z/5xW8zcGhP):
int foo(int x, int y) {
return x + y + 1;
}
int bar(int x, int y) {
return x + y + 2;
}
extern bool use_bar;
typedef int (*function_pointer)(int, int);
static function_pointer resolver (void) {
return use_bar ? bar : foo;
}
__attribute__((ifunc("resolver")))
int call_ifunc(int x, int y);
>> clang ../bug/ifunc-\#59164.cpp
../bug/ifunc-#59164.cpp:17:16: error: ifunc must point to a defined function
__attribute__((ifunc("resolver")))
^
1 error generated.
FWIW, it seems GCC doesn't accept this either: https://godbolt.org/z/9cWYYhxE1
Hello,
I am interested in working on this issue. It seems that the place to fix is
CodeGenModule::checkAliasedGlobal()(https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L351-L354).However, I am not sure how to lookup a non-mangled name in
CodeGenModule, as I cannot see a method for it. I imagine the fix is something like "lookup given name to find symbols -> see if their mangled name exists in the current TU -> provide fix as a note", but I am not sure how to implement it.
@dwblaikie Can you give me any pointers on how to go about implementing this if it is the correct approach? If not, what should I do?
Trying to compile this sample
ifuncprogram produces an unhelpful error:int foo(int x, int y) { return x + y + 1; } int bar(int x, int y) { return x + y + 2; } extern bool use_bar; static void (*resolver (void)) (void) { return use_bar ? (void(*)()) use_bar : (void(*)())foo; } __attribute__((ifunc("resolver"))) int call_ifunc(int x, int y);$ clang++ ifunc.cpp ifunc.cpp:15:16: error: ifunc must point to a defined function __attribute__((ifunc("resolver"))) ^ 1 error generated.This is apparently due to this expecting the mangled name of the function. It compiles if I trade static for extern "C". It would be better if it noticed you were trying to call a function with mangling and inform how to fix it.
Check the link of the gold bolt I am sharing with you https://godbolt.org/z/nsGnejqTY
The code you provide there is nothing wrong with it but several improvements can be made to it.
The code you have provided look to be a C code that uses an indirect function (ifunc) feature of the GCC compiler. The code declares two functions 1.foo 2. bar, a boolean variable use_bar, a typedef for a function pointer function_pointer, and a resolver function that returns either bar or foo depending on the value of use_bar.
There do not seem to be any syntax errors or logical issues with the code. However, there are a few things that can be improved:
The resolver function should have a proper return type. It can be declared as function_pointer resolver(void).
The use of attribute((ifunc("resolver"))) is not portable and is specific to the GCC compiler. If you need to use this code with other compilers, you should consider using a different approach to implement the indirect function.
The use_bar variable is not defined anywhere in the code. You need to either declare it with a value or include a header file that defines it.
#include <stdbool.h>
- int foo(int x, int y) {
- return x + y + 1; 3.}
4.int bar(int x, int y) { 5. return x + y + 2; 6.}
7.extern bool use_bar;
8.typedef int (*function_pointer)(int, int);
9.static function_pointer resolver(void) { 10. return use_bar ? bar : foo; } 11__attribute__((ifunc("resolver"))) 12. int call_ifunc(int x, int y);
But this only work with the C compiler not with the C++.
. The attribute syntax and the use of indirect functions with the GCC compiler are specific to C and may not be supported by the C++ compiler.
If you need to use this code with a C++ compiler, you will need to make some changes to the code to make it compatible with C++. Here is a corrected version of the code in C++:
#include <stdbool.h>
int foo(int x, int y) { return x + y + 1; }
int bar(int x, int y) { return x + y + 2; }
extern bool use_bar;
typedef int (*function_pointer)(int, int);
function_pointer resolver() { return use_bar ? bar : foo; }
int call_ifunc(int x, int y) { return resolver()(x, y); }
Here is the link you can see : https://godbolt.org/z/6rzjooMGK
@aabhinavg - I appreciate the thought, though these tips probably aren't especially relevant to this bug, which is about the diagnostic experience if those features are used. (& also seems to miss the point/value in using ifuncs - which is that the resolving happens once at program load time, rather than every time the function is called - yes, if someone doesn't need that extra sliver of performance, they can write more portable code, but if they're using ifunc it's probably because they've already chosen the higher performance/less portable tradeoff)
Hello, I am interested in working on this issue. It seems that the place to fix is
CodeGenModule::checkAliasedGlobal()(https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L351-L354). However, I am not sure how to lookup a non-mangled name inCodeGenModule, as I cannot see a method for it. I imagine the fix is something like "lookup given name to find symbols -> see if their mangled name exists in the current TU -> provide fix as a note", but I am not sure how to implement it.@dwblaikie Can you give me any pointers on how to go about implementing this if it is the correct approach? If not, what should I do?
@dwblaikie Sorry for irresponsibly pinging, but I think you may have missed my reply? Appreciate any response you may have.
Hello, I am interested in working on this issue. It seems that the place to fix is
CodeGenModule::checkAliasedGlobal()(https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/CodeGenModule.cpp#L351-L354). However, I am not sure how to lookup a non-mangled name inCodeGenModule, as I cannot see a method for it. I imagine the fix is something like "lookup given name to find symbols -> see if their mangled name exists in the current TU -> provide fix as a note", but I am not sure how to implement it.@dwblaikie Can you give me any pointers on how to go about implementing this if it is the correct approach? If not, what should I do?
@dwblaikie Sorry for irresponsibly pinging, but I think you may have missed my reply? Appreciate any response you may have.
Sure sure - yeah, I don't know exactly how to do that lookup either. I wonder if there's something else we could look for to learn from...
(hmm, looks like GCC and Clang share this issue, and other attributes (like alias/weak) have the same problem in terms of doing lookup for a raw mangled name - though Clang's enable_if attribute does name lookup (I think it takes a whole expression, maybe, so makes sense it does name lookup there) - rather than a string resolved at codegen time)
Usually name lookup has to be done in a DeclContext, which may not exist by the time we're in codegen...
Might be the simplest thing to do would be to include text in the diagnostic that mentions that a mangled name is required (especially/if only when compiling in non-C mode or only in C++ mode) without actually doing the lookup.
If you want to try doing the lookup - might be worth posting to LLVM Discord or Discourse for tips on how to do that. I spent a while looking around and figure maybe it's something like clang::Sema::LookupParsedName
Based on my discussion on discord (https://discord.com/channels/636084430946959380/636732781086638081/1073563320424071270), I have tried to create an initial fix for this: https://reviews.llvm.org/D143803