source: icGREP/icgrep-devel/icgrep/IR_Gen/idisa_target.cpp @ 5871

Last change on this file since 5871 was 5871, checked in by cameron, 19 months ago

AVX 512 detection and IDISA builder

File size: 3.1 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5
6#include "idisa_target.h"
7#include <toolchain/toolchain.h>
8#include <IR_Gen/idisa_sse_builder.h>
9#include <IR_Gen/idisa_avx_builder.h>
10#include <IR_Gen/idisa_i64_builder.h>
11#include <IR_Gen/idisa_nvptx_builder.h>
12#include <llvm/IR/Module.h>
13#include <llvm/ADT/Triple.h>
14#include <llvm/Support/ErrorHandling.h>
15#include <llvm/Support/raw_ostream.h>
16#include <kernels/kernel_builder.h>
17
18using namespace kernel;
19using namespace llvm;
20
21struct Features {
22    bool hasAVX;
23    bool hasAVX2;
24    bool hasAVX512BW;
25    Features() : hasAVX(0), hasAVX2(0), hasAVX512BW(0) { }
26};
27
28Features getHostCPUFeatures() {
29    Features hostCPUFeatures;
30    StringMap<bool> features;
31    if (sys::getHostCPUFeatures(features)) {
32        hostCPUFeatures.hasAVX = features.lookup("avx");
33        hostCPUFeatures.hasAVX2 = features.lookup("avx2");
34        hostCPUFeatures.hasAVX512BW = features.lookup("avx512bw");
35        //if (hostCPUFeatures.hasAVX512BW) errs() << "AVX512BW detected.\n";
36    }
37    return hostCPUFeatures;
38}
39
40bool AVX2_available() {
41    StringMap<bool> features;
42    if (sys::getHostCPUFeatures(features)) {
43        return features.lookup("avx2");
44    }
45    return false;
46}
47
48bool AVX512BW_available() {
49    StringMap<bool> features;
50    if (sys::getHostCPUFeatures(features)) {
51        return features.lookup("avx512bw");
52    }
53    return false;
54}
55
56namespace IDISA {
57   
58KernelBuilder * GetIDISA_Builder(llvm::LLVMContext & C) {
59    const auto hostCPUFeatures = getHostCPUFeatures();
60    if (LLVM_LIKELY(codegen::BlockSize == 0)) {  // No BlockSize override: use processor SIMD width
61       
62        if (hostCPUFeatures.hasAVX512BW) codegen::BlockSize = 512;
63        else if (hostCPUFeatures.hasAVX2) codegen::BlockSize = 256;
64        else codegen::BlockSize = 128;
65    }
66    else if (((codegen::BlockSize & (codegen::BlockSize - 1)) != 0) || (codegen::BlockSize < 64)) {
67        llvm::report_fatal_error("BlockSize must be a power of 2 and >=64");
68    }
69    if (codegen::BlockSize >= 512) {
70        // AVX512BW builder can only be used for BlockSize multiples of 512
71        if (hostCPUFeatures.hasAVX512BW) {
72            return new KernelBuilderImpl<IDISA_AVX512BW_Builder>(C, codegen::BlockSize, codegen::BlockSize);
73        }
74    }
75    if (codegen::BlockSize >= 256) {
76        // AVX2 or AVX builders can only be used for BlockSize multiples of 256
77        if (hostCPUFeatures.hasAVX2) {
78            return new KernelBuilderImpl<IDISA_AVX2_Builder>(C, codegen::BlockSize, codegen::BlockSize);
79        } else if (hostCPUFeatures.hasAVX) {
80            return new KernelBuilderImpl<IDISA_AVX_Builder>(C, codegen::BlockSize, codegen::BlockSize);
81        }
82    } else if (codegen::BlockSize == 64) {
83        return new KernelBuilderImpl<IDISA_I64_Builder>(C, codegen::BlockSize, codegen::BlockSize);
84    }
85    return new KernelBuilderImpl<IDISA_SSE2_Builder>(C, codegen::BlockSize, codegen::BlockSize);
86}
87#ifdef CUDA_ENABLED
88KernelBuilder * GetIDISA_GPU_Builder(llvm::LLVMContext & C) {
89    return new KernelBuilderImpl<IDISA_NVPTX20_Builder>(C, 64, 64 * 64);
90}
91#endif
92}
Note: See TracBrowser for help on using the repository browser.