OpenMP编译成LLVMIR

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

介绍

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

方法

这是一个openmp程序


#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代码:


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

IR代码如下:


; 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中的代码,该函数的代码就是对应并行域中的代码。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 引言 维基百科:编译语言(英语:Compiled language)是一种以编译器来实现的编程语言。它不像解释型语...
    Flame_Dream阅读 8,634评论 5 52
  • 编译器做些什么? 本文主要探讨一下编译器主要做些什么,以及如何有效的利用编译器。 简单的说,编译器有两个职责:把 ...
    评评分分阅读 1,187评论 1 5
  • http://www.starming.com/index.php?v=index&view=107 http:/...
    111浪子111阅读 3,222评论 0 11
  • 在 iOS 开发的过程中,Xcode 为我们提供了非常完善的编译能力,正常情况下,我们只需要 Command + ...
    CoderLF阅读 13,188评论 0 17
  • 我是孤独的,至今,没能够找到知己,志同道合者,这也许是我的原因吧! 曾今,我十分惧怕一个人,一个人吃饭,逛街,走路...
    凡莹阅读 190评论 0 0