Skip to content

Commit 71c6689

Browse files
author
Chip-Kerchner
committed
Fix dynamic dispatch to work for clang.
1 parent c60f9d9 commit 71c6689

1 file changed

Lines changed: 59 additions & 82 deletions

File tree

driver/others/dynamic_power.c

Lines changed: 59 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ char *gotoblas_corename(void) {
3636
return corename[0];
3737
}
3838

39-
#if defined(__clang__) && !defined(_AIX)
40-
static int __builtin_cpu_supports(char* arg)
41-
{
42-
return 0;
43-
}
44-
#endif
45-
4639
#define CPU_UNKNOWN 0
4740
#define CPU_POWER5 5
4841
#define CPU_POWER6 6
@@ -51,7 +44,31 @@ static int __builtin_cpu_supports(char* arg)
5144
#define CPU_POWER9 9
5245
#define CPU_POWER10 10
5346

54-
#if defined(C_PGI) || (defined(__clang__) && !defined(_AIX))
47+
#ifdef _AIX
48+
#include <sys/systemcfg.h>
49+
50+
static int cpuid(void)
51+
{
52+
int arch = _system_configuration.implementation;
53+
#ifdef POWER_6
54+
if (arch == POWER_6) return CPU_POWER6;
55+
#endif
56+
#ifdef POWER_7
57+
else if (arch == POWER_7) return CPU_POWER7;
58+
#endif
59+
#ifdef POWER_8
60+
else if (arch == POWER_8) return CPU_POWER8;
61+
#endif
62+
#ifdef POWER_9
63+
else if (arch == POWER_9) return CPU_POWER9;
64+
#endif
65+
#ifdef POWER_10
66+
else if (arch == POWER_10) return CPU_POWER10;
67+
#endif
68+
return CPU_UNKNOWN;
69+
}
70+
#else
71+
#if defined(C_PGI) || defined(__clang__)
5572
/*
5673
* NV HPC compilers do not yet implement __builtin_cpu_is().
5774
* Fake a version here for use in the CPU detection code below.
@@ -61,8 +78,6 @@ static int __builtin_cpu_supports(char* arg)
6178
* what was requested.
6279
*/
6380

64-
#include <string.h>
65-
6681
/*
6782
* Define POWER processor version table.
6883
*
@@ -161,99 +176,61 @@ static struct {
161176
},
162177
};
163178

164-
static int __builtin_cpu_is(const char *cpu) {
165-
int i;
166-
uint32_t pvr;
167-
uint32_t cpu_type;
179+
static int cpuid(void)
180+
{
181+
int i;
182+
uint32_t pvr;
183+
uint32_t cpu_type;
168184

169-
asm("mfpvr %0" : "=r"(pvr));
185+
asm("mfpvr %0" : "=r"(pvr));
170186

171-
for (i = 0 ; i < sizeof pvrPOWER / sizeof *pvrPOWER ; ++i) {
172-
if ((pvr & pvrPOWER[i].pvr_mask) == pvrPOWER[i].pvr_value) {
173-
break;
174-
}
175-
}
187+
for (i = 0 ; i < sizeof pvrPOWER / sizeof *pvrPOWER ; ++i) {
188+
if ((pvr & pvrPOWER[i].pvr_mask) == pvrPOWER[i].pvr_value) {
189+
break;
190+
}
191+
}
176192

177193
#if defined(DEBUG)
178-
printf("%s: returning CPU=%s, cpu_type=%p\n", __func__,
179-
pvrPOWER[i].cpu_name, pvrPOWER[i].cpu_type);
194+
printf("%s: returning CPU=%s, cpu_type=%p\n", __func__,
195+
pvrPOWER[i].cpu_name, pvrPOWER[i].cpu_type);
180196
#endif
181-
cpu_type = pvrPOWER[i].cpu_type;
182-
183-
if (!strcmp(cpu, "power8"))
184-
return cpu_type == CPU_POWER8;
185-
if (!strcmp(cpu, "power9"))
186-
return cpu_type == CPU_POWER9;
187-
return 0;
197+
cpu_type = pvrPOWER[i].cpu_type;
198+
return (int)(cpu_type);
188199
}
189-
190200
#endif /* C_PGI */
191-
192-
#ifdef _AIX
193-
#include <sys/systemcfg.h>
194-
195-
static int cpuid(void)
196-
{
197-
int arch = _system_configuration.implementation;
198-
#ifdef POWER_6
199-
if (arch == POWER_6) return CPU_POWER6;
200-
#endif
201-
#ifdef POWER_7
202-
else if (arch == POWER_7) return CPU_POWER7;
203-
#endif
204-
#ifdef POWER_8
205-
else if (arch == POWER_8) return CPU_POWER8;
206-
#endif
207-
#ifdef POWER_9
208-
else if (arch == POWER_9) return CPU_POWER9;
209-
#endif
210-
#ifdef POWER_10
211-
else if (arch == POWER_10) return CPU_POWER10;
212-
#endif
213-
return CPU_UNKNOWN;
214-
}
201+
#endif /* _AIX */
215202

216203
#ifndef __BUILTIN_CPU_SUPPORTS__
217-
static int __builtin_cpu_supports(const char* arg)
218-
{
219-
static int ipinfo = -1;
220-
if (ipinfo < 0) {
221-
ipinfo = cpuid();
222-
}
223-
if (ipinfo >= CPU_POWER10) {
224-
if (!strcmp(arg, "power10")) return 1;
225-
}
226-
if (ipinfo >= CPU_POWER9) {
227-
if (!strcmp(arg, "power9")) return 1;
228-
}
229-
if (ipinfo >= CPU_POWER8) {
230-
if (!strcmp(arg, "power8")) return 1;
231-
}
232-
if (ipinfo >= CPU_POWER6) {
233-
if (!strcmp(arg, "power6")) return 1;
234-
}
235-
return 0;
236-
}
204+
#include <string.h>
237205

238206
static int __builtin_cpu_is(const char *arg)
239207
{
240208
static int ipinfo = -1;
241209
if (ipinfo < 0) {
242210
ipinfo = cpuid();
243211
}
212+
#ifdef HAVE_P10_SUPPORT
244213
if (ipinfo == CPU_POWER10) {
245214
if (!strcmp(arg, "power10")) return 1;
246-
} else if (ipinfo == CPU_POWER9) {
215+
}
216+
#endif
217+
if (ipinfo == CPU_POWER9) {
247218
if (!strcmp(arg, "power9")) return 1;
248219
} else if (ipinfo == CPU_POWER8) {
249220
if (!strcmp(arg, "power8")) return 1;
221+
#ifndef C_PGI
250222
} else if (ipinfo == CPU_POWER6) {
251223
if (!strcmp(arg, "power6")) return 1;
224+
#endif
252225
}
253226
return 0;
254227
}
228+
229+
static int __builtin_cpu_supports(const char *arg)
230+
{
231+
return 0;
232+
}
255233
#endif
256-
#endif /* _AIX */
257234

258235
static gotoblas_t *get_coretype(void) {
259236

@@ -268,18 +245,18 @@ static gotoblas_t *get_coretype(void) {
268245
return &gotoblas_POWER9;
269246
#endif
270247
#ifdef HAVE_P10_SUPPORT
271-
#ifdef _AIX
272-
if (__builtin_cpu_supports("power10"))
248+
#if defined(_AIX) || defined(__clang__)
249+
if (__builtin_cpu_is("power10"))
273250
#else
274251
if (__builtin_cpu_supports ("arch_3_1") && __builtin_cpu_supports ("mma"))
275252
#endif
276253
return &gotoblas_POWER10;
277254
#endif
278-
/* Fall back to the POWER9 implementation if the toolchain is too old or the MMA feature is not set */
255+
/* Fall back to the POWER9 implementation if the toolchain is too old or the MMA feature is not set */
279256
#if (!defined __GNUC__) || ( __GNUC__ >= 11) || (__GNUC__ == 10 && __GNUC_MINOR__ >= 2)
280-
if (__builtin_cpu_is("power10"))
281-
return &gotoblas_POWER9;
282-
#endif
257+
if (__builtin_cpu_is("power10"))
258+
return &gotoblas_POWER9;
259+
#endif
283260
return NULL;
284261
}
285262

0 commit comments

Comments
 (0)