Skip to content

Commit

Permalink
[LoongArch][clang] Add support for option -msimd= and macro `__loon…
Browse files Browse the repository at this point in the history
…garch_simd_width`. (#97984)
  • Loading branch information
ylzsx authored Jul 9, 2024
1 parent bb8998d commit 626c7ce
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 2 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,8 @@ def err_drv_loongarch_wrong_fpu_width : Error<
"wrong fpu width; %select{LSX|LASX}0 depends on 64-bit FPU">;
def err_drv_loongarch_invalid_simd_option_combination : Error<
"invalid option combination; LASX depends on LSX">;
def err_drv_loongarch_invalid_msimd_EQ : Error<
"invalid argument '%0' to -msimd=; must be one of: none, lsx, lasx">;

def err_drv_expand_response_file : Error<
"failed to expand response file: %0">;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5252,6 +5252,9 @@ def mlasx : Flag<["-"], "mlasx">, Group<m_loongarch_Features_Group>,
HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">;
def mno_lasx : Flag<["-"], "mno-lasx">, Group<m_loongarch_Features_Group>,
HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">;
def msimd_EQ : Joined<["-"], "msimd=">, Group<m_loongarch_Features_Group>,
Flags<[TargetSpecific]>,
HelpText<"Select the SIMD extension(s) to be enabled in LoongArch either 'none', 'lsx', 'lasx'.">;
def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">,
Visibility<[ClangOption, CC1Option]>, Group<m_Group>,
MarshallingInfoFlag<CodeGenOpts<"MNopMCount">>;
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Basic/Targets/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,14 @@ void LoongArchTargetInfo::getTargetDefines(const LangOptions &Opts,
TuneCPU = ArchName;
Builder.defineMacro("__loongarch_tune", Twine('"') + TuneCPU + Twine('"'));

if (HasFeatureLSX)
if (HasFeatureLASX) {
Builder.defineMacro("__loongarch_simd_width", "256");
Builder.defineMacro("__loongarch_sx", Twine(1));
if (HasFeatureLASX)
Builder.defineMacro("__loongarch_asx", Twine(1));
} else if (HasFeatureLSX) {
Builder.defineMacro("__loongarch_simd_width", "128");
Builder.defineMacro("__loongarch_sx", Twine(1));
}

StringRef ABI = getABI();
if (ABI == "lp64d" || ABI == "lp64f" || ABI == "lp64s")
Expand Down
29 changes: 29 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/LoongArch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ void loongarch::getLoongArchTargetFeatures(const Driver &D,
} else /*-mno-lasx*/
Features.push_back("-lasx");
}

// Select lsx/lasx feature determined by -msimd=.
// Option -msimd= has lower priority than -m[no-]lsx and -m[no-]lasx.
if (const Arg *A = Args.getLastArg(options::OPT_msimd_EQ)) {
StringRef MSIMD = A->getValue();
if (MSIMD == "lsx") {
// Option -msimd=lsx depends on 64-bit FPU.
// -m*-float and -mfpu=none/0/32 conflict with -mlsx.
if (llvm::find(Features, "-d") != Features.end())
D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LSX*/ 0;
// The previous option does not contain feature -lsx.
else if (llvm::find(Features, "-lsx") == Features.end())
Features.push_back("+lsx");
} else if (MSIMD == "lasx") {
// Option -msimd=lasx depends on 64-bit FPU and LSX.
// -m*-float and -mfpu=none/0/32 conflict with -mlsx.
if (llvm::find(Features, "-d") != Features.end())
D.Diag(diag::err_drv_loongarch_wrong_fpu_width) << /*LASX*/ 1;
else if (llvm::find(Features, "-lsx") != Features.end())
D.Diag(diag::err_drv_loongarch_invalid_simd_option_combination);
// The previous option does not contain feature -lasx.
else if (llvm::find(Features, "-lasx") == Features.end()) {
Features.push_back("+lsx");
Features.push_back("+lasx");
}
} else if (MSIMD != "none") {
D.Diag(diag::err_drv_loongarch_invalid_msimd_EQ) << MSIMD;
}
}
}

std::string loongarch::postProcessTargetCPUString(const std::string &CPU,
Expand Down
129 changes: 129 additions & 0 deletions clang/test/Driver/loongarch-msimd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/// Test -msimd options.

/// COM: -msimd=none
// RUN: %clang --target=loongarch64 -mlasx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX

// RUN: %clang --target=loongarch64 -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX

// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mlsx -msimd=none -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX


/// COM: -msimd=lsx
// RUN: %clang --target=loongarch64 -mlasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX

// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -mno-lsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX

// RUN: %clang --target=loongarch64 -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX


/// COM: -msimd=lasx
// RUN: %clang --target=loongarch64 -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX
// RUN: %clang --target=loongarch64 -mlasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX
// RUN: %clang --target=loongarch64 -mlasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX
// RUN: %clang --target=loongarch64 -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,LASX

// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=NOLSX,NOLASX

// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -msimd=lasx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX
// RUN: %clang --target=loongarch64 -mlasx -mno-lasx -mlsx -msimd=lsx -fsyntax-only %s -### 2>&1 | \
// RUN: grep -o '"-target-feature" "+[[:alnum:]]\+"' | sort -r | \
// RUN: FileCheck %s --check-prefixes=LSX,NOLASX


// LSX: "-target-feature" "+lsx"
// LASX: "-target-feature" "+lasx"
// NOLSX-NOT: "-target-feature" "+lsx"
// NOLASX-NOT: "-target-feature" "+lasx"
3 changes: 3 additions & 0 deletions clang/test/Preprocessor/init-loongarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@
// RUN: %clang --target=loongarch64 -mno-lasx -mlsx -x c -E -dM %s -o - \
// RUN: | FileCheck --match-full-lines --check-prefix=MLSX %s
// MLSX-NOT: #define __loongarch_asx
// MLSX: #define __loongarch_simd_width 128
// MLSX: #define __loongarch_sx 1

// RUN: %clang --target=loongarch64 -mlasx -x c -E -dM %s -o - \
Expand All @@ -838,6 +839,7 @@
// RUN: %clang --target=loongarch64 -mlasx -mlsx -x c -E -dM %s -o - \
// RUN: | FileCheck --match-full-lines --check-prefix=MLASX %s
// MLASX: #define __loongarch_asx 1
// MLASX: #define __loongarch_simd_width 256
// MLASX: #define __loongarch_sx 1

// RUN: %clang --target=loongarch64 -mno-lsx -x c -E -dM %s -o - \
Expand All @@ -851,4 +853,5 @@
// RUN: %clang --target=loongarch64 -mno-lasx -x c -E -dM %s -o - \
// RUN: | FileCheck --match-full-lines --check-prefix=MNO-LSX %s
// MNO-LSX-NOT: #define __loongarch_asx
// MNO-LSX-NOT: #define __loongarch_simd_width
// MNO-LSX-NOT: #define __loongarch_sx

0 comments on commit 626c7ce

Please sign in to comment.