https://clang-omp.github.io/

介绍

目前clang/llvm编译器已经支持OpenMP,目前,OpenMP 3.1已经被clang/llvm 3.7完全支持。

方法

这是一个openmp程序

1
2
3
4
5
6
7
8
9
10
11
12

#include <omp.h>

#include <stdio.h>

int main() {

#pragma omp parallel

printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());

}

将更改程序转化为相应的IR代码:

1
2

clang -fopenmp -emit-llvm -S test.c -o test.ll

IR代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

; ModuleID = 'test.bc'

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

target triple = "x86_64-pc-linux-gnu"

%ident_t = type { i32, i32, i32, i32, i8* }

@.str = private unnamed_addr constant [35 x i8] c"Hello from thread %d, nthreads %d\0A\00", align 1

@.str.1 = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00", align 1

@0 = private unnamed_addr constant %ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i32 0, i32 0) }, align 8

; Function Attrs: nounwind uwtable

define i32 @main() #0 {

  call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...) @__kmpc_fork_call(%ident_t* @0, i32 0, void (i32*, i32*, ...)* bitcast (void (i32*, i32*)* @.omp_outlined. to void (i32*, i32*, ...)*))

  ret i32 0

}

; Function Attrs: nounwind uwtable

define internal void @.omp_outlined.(i32* noalias %.global_tid., i32* noalias %.bound_tid.) #0 {

  %1 = alloca i32*, align 8

  %2 = alloca i32*, align 8

  store i32* %.global_tid., i32** %1, align 8

  store i32* %.bound_tid., i32** %2, align 8

  %3 = call i32 @omp_get_thread_num()

  %4 = call i32 @omp_get_num_threads()

  %5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i32 0, i32 0), i32 %3, i32 %4)

  ret void

}

declare i32 @printf(i8*, ...) #1

declare i32 @omp_get_thread_num() #1

declare i32 @omp_get_num_threads() #1

declare void @__kmpc_fork_call(%ident_t*, i32, void (i32*, i32*, ...)*, ...)

attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

attributes #1 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)"}

其中mpc_fork_call是OpenMP定义的运行时函数,用来生成线程池,每个线程将执行函数omp_outlined中的代码,该函数的代码就是对应并行域中的代码。

Over!!!