Clang 版本
$ clang --version
clang version 7.0.1-12 (tags/RELEASE_701/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
使用Clang的例子
编写一个简单的c代码
// t.c
typedef float V __attribute__((vector_size(16)));
V foo(V a, V b) { return a+b*a; }
__attribute__ 用于属性声明。
vector_size(16) 声明V的大小为16字节
预处理
$ clang t.c -E
# 1 "t.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 349 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "t.c" 2
typedef float V __attribute__((vector_size(16)));
V foo(V a, V b) { return a+b*a;}
类型检查
$ clang -fsyntax-only t.c
这里什么都没有输出
GCC 选项
clang -fsyntax-only t.c -pedantic
这里也什么都没有输出。就很奇怪,和clang官方文档不符合.
完美打印抽象语法树
$ clang -cc1 t.c -ast-print
typedef attribute((vector_size(4 * sizeof(float)))) float V;
V foo(V a, V b) {
return a + b * a;
}
请注意,-cc1 参数表示应该运行编译器前端,而不是驱动程序。 编译器前端有几个额外的 Clang 特定功能,这些功能未通过 GCC 兼容驱动程序接口公开。
使用LLVM代码生成
$ clang t.c -S -emit-llvm -o -
; ModuleID = 't.c'
source_filename = "t.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
; Function Attrs: noinline nounwind optnone uwtable
define dso_local <4 x float> @foo(<4 x float>, <4 x float>) #0 {
%3 = alloca <4 x float>, align 16
%4 = alloca <4 x float>, align 16
store <4 x float> %0, <4 x float>* %3, align 16
store <4 x float> %1, <4 x float>* %4, align 16
%5 = load <4 x float>, <4 x float>* %3, align 16
%6 = load <4 x float>, <4 x float>* %4, align 16
%7 = load <4 x float>, <4 x float>* %3, align 16
%8 = fmul <4 x float> %6, %7
%9 = fadd <4 x float> %5, %8
ret <4 x float> %9
}
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "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-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.1-12 (tags/RELEASE_701/final)"}
clang -fomit-frame-pointer -O3 -S -o - t.c # On x86_64
.text
.file "t.c"
.globl foo # -- Begin function foo
.p2align 4, 0x90
.type foo,@function
foo: # @foo
.cfi_startproc
# %bb.0:
mulps %xmm0, %xmm1
addps %xmm1, %xmm0
retq
.Lfunc_end0:
.size foo, .Lfunc_end0-foo
.cfi_endproc
# -- End function
.ident "clang version 7.0.1-12 (tags/RELEASE_701/final)"
.section ".note.GNU-stack","",@progbits
.addrsig