Clang的使用

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

参考文献

Getting Started: Building and Running Clang

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值