[clang] [LoongArch][clang] Add support for option `-msimd` and macro ` loonga… (PR #97984)
https://github.com/ylzsx created https://github.com/llvm/llvm-project/pull/97984 …rch_simd_width`. >From bd70e74419947c32265d698163722552e80df12f Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Fri, 5 Jul 2024 10:40:07 +0800 Subject: [PATCH] [LoongArch][clang] Add support for option `-msimd` and macro ` loongarch_simd_width`. --- .../clang/Basic/DiagnosticDriverKinds.td | 2 + clang/include/clang/Driver/Options.td | 3 + clang/lib/Basic/Targets/LoongArch.cpp | 8 +- .../lib/Driver/ToolChains/Arch/LoongArch.cpp | 29 clang/test/Driver/loongarch-msimd.c | 129 ++ clang/test/Preprocessor/init-loongarch.c | 3 + 6 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 clang/test/Driver/loongarch-msimd.c diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td index a62bdc21298ee..243d88d53d664 100644 --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -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">; diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 58ca6f2bea9e4..707ce8ce78d0b 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -5252,6 +5252,9 @@ def mlasx : Flag<["-"], "mlasx">, Group, HelpText<"Enable Loongson Advanced SIMD Extension (LASX).">; def mno_lasx : Flag<["-"], "mno-lasx">, Group, HelpText<"Disable Loongson Advanced SIMD Extension (LASX).">; +def msimd_EQ : Joined<["-"], "msimd=">, 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, MarshallingInfoFlag>; diff --git a/clang/lib/Basic/Targets/LoongArch.cpp b/clang/lib/Basic/Targets/LoongArch.cpp index 280bd1d8033cc..75f71a337b7a4 100644 --- a/clang/lib/Basic/Targets/LoongArch.cpp +++ b/clang/lib/Basic/Targets/LoongArch.cpp @@ -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") diff --git a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp index 9ea4cc3f7cb95..4a2b9efc9ffad 100644 --- a/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp +++ b/clang/lib/Driver/ToolChains/Arch/LoongArch.cpp @@ -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
[clang] [LoongArch][clang] Add support for option `-msimd` and macro `__loongarch_simd_width`. (PR #97984)
https://github.com/ylzsx edited https://github.com/llvm/llvm-project/pull/97984 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LoongArch][clang] Add support for option `-msimd` and macro `__loongarch_simd_width`. (PR #97984)
https://github.com/ylzsx edited https://github.com/llvm/llvm-project/pull/97984 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [LoongArch][clang] Modify `loongarch-msimd.c` to avoid `grep -o`. NFC (PR #98442)
https://github.com/ylzsx created https://github.com/llvm/llvm-project/pull/98442 https://lab.llvm.org/buildbot/#/builders/64/builds/250/steps/6/logs/FAIL__Clang__loongarch-msimd_c >From 238cbc3a0e5f7a407e4512fb5ac0d33fb7d65150 Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Thu, 11 Jul 2024 15:29:44 +0800 Subject: [PATCH] [LoongArch][clang] Modify `loongarch-msimd.c` to avoid `grep -o`. NFC https://lab.llvm.org/buildbot/#/builders/64/builds/250/steps/6/logs/FAIL__Clang__loongarch-msimd_c --- clang/test/Driver/loongarch-msimd.c | 42 +++-- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/clang/test/Driver/loongarch-msimd.c b/clang/test/Driver/loongarch-msimd.c index 984f3e8bf2bfc..cd463300c8747 100644 --- a/clang/test/Driver/loongarch-msimd.c +++ b/clang/test/Driver/loongarch-msimd.c @@ -2,128 +2,94 @@ /// 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 '"-targe
[clang] [LoongArch][clang] Add support for option `-msimd=` and macro `__loongarch_simd_width`. (PR #97984)
ylzsx wrote: > Hi, this test is failing on the AIX bot > https://lab.llvm.org/buildbot/#/builders/64/builds/250/steps/6/logs/FAIL__Clang__loongarch-msimd_c > > The reason is that -o is not a supported option with grep on AIX. Would you > be able to adapt the test to avoid piping and use input redirection (so that > the builtin grep is used, for example here: > https://github.com/llvm/llvm-project/pull/83184/files. I'm not sure if this > works with grep though) or use another command like sed? @jakeegan Already fixed, see https://github.com/llvm/llvm-project/pull/98442. https://github.com/llvm/llvm-project/pull/97984 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang][LoongArch] Emit target features for Loongarch64. (PR #114735)
https://github.com/ylzsx created https://github.com/llvm/llvm-project/pull/114735 None >From dd4aca55c1e35ebd6dbb9d849ba9de8b930ae209 Mon Sep 17 00:00:00 2001 From: ylzsx <2375355...@qq.com> Date: Sat, 2 Nov 2024 10:48:29 +0800 Subject: [PATCH] [Flang][LoongArch] Emit target features for Loongarch64. --- clang/lib/Driver/ToolChains/Flang.cpp | 3 +++ flang/test/Driver/target-cpu-features.f90 | 6 ++ 2 files changed, 9 insertions(+) diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index f9d2fdffe3b2fc..11070c23c75f4a 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -414,6 +414,9 @@ void Flang::addTargetOptions(const ArgList &Args, case llvm::Triple::ppc64le: AddPPCTargetArgs(Args, CmdArgs); break; + case llvm::Triple::loongarch64: +getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false); +break; } if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { diff --git a/flang/test/Driver/target-cpu-features.f90 b/flang/test/Driver/target-cpu-features.f90 index e3eb8491058c7f..5a3fd0d8380027 100644 --- a/flang/test/Driver/target-cpu-features.f90 +++ b/flang/test/Driver/target-cpu-features.f90 @@ -41,6 +41,9 @@ ! RUN: %flang --target=r600-unknown-unknown -mcpu=cayman -nogpulib -c %s -### 2>&1 \ ! RUN: | FileCheck %s -check-prefix=CHECK-AMDGPU-R600 +! RUN: %flang --target=loongarch64-linux-gnu -c %s -### 2>&1 \ +! RUN: | FileCheck %s -check-prefix=CHECK-LOONGARCH64 + ! CHECK-A57: "-fc1" "-triple" "aarch64-unknown-linux-gnu" ! CHECK-A57-SAME: "-target-cpu" "cortex-a57" ! CHECK-A57-SAME: "-target-feature" "+v8a" "-target-feature" "+aes" "-target-feature" "+crc" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+perfmon" "-target-feature" "+sha2 @@ -86,3 +89,6 @@ ! CHECK-AMDGPU-R600: "-fc1" "-triple" "r600-unknown-unknown" ! CHECK-AMDGPU-R600-SAME: "-target-cpu" "cayman" + +! CHECK-LOONGARCH64: "-fc1" "-triple" "loongarch64-unknown-linux-gnu" +! CHECK-LOONGARCH64-SAME: "-target-cpu" "loongarch64" "-target-feature" "+lsx" "-target-feature" "+64bit" "-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+ual" ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang][LoongArch] Emit target features for Loongarch64. (PR #114735)
ylzsx wrote: Ping https://github.com/llvm/llvm-project/pull/114735 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang][LoongArch] Emit target features for Loongarch64. (PR #114735)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/114735 >From dd4aca55c1e35ebd6dbb9d849ba9de8b930ae209 Mon Sep 17 00:00:00 2001 From: ylzsx <2375355...@qq.com> Date: Sat, 2 Nov 2024 10:48:29 +0800 Subject: [PATCH 1/2] [Flang][LoongArch] Emit target features for Loongarch64. --- clang/lib/Driver/ToolChains/Flang.cpp | 3 +++ flang/test/Driver/target-cpu-features.f90 | 6 ++ 2 files changed, 9 insertions(+) diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index f9d2fdffe3b2fc..11070c23c75f4a 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -414,6 +414,9 @@ void Flang::addTargetOptions(const ArgList &Args, case llvm::Triple::ppc64le: AddPPCTargetArgs(Args, CmdArgs); break; + case llvm::Triple::loongarch64: +getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false); +break; } if (Arg *A = Args.getLastArg(options::OPT_fveclib)) { diff --git a/flang/test/Driver/target-cpu-features.f90 b/flang/test/Driver/target-cpu-features.f90 index e3eb8491058c7f..5a3fd0d8380027 100644 --- a/flang/test/Driver/target-cpu-features.f90 +++ b/flang/test/Driver/target-cpu-features.f90 @@ -41,6 +41,9 @@ ! RUN: %flang --target=r600-unknown-unknown -mcpu=cayman -nogpulib -c %s -### 2>&1 \ ! RUN: | FileCheck %s -check-prefix=CHECK-AMDGPU-R600 +! RUN: %flang --target=loongarch64-linux-gnu -c %s -### 2>&1 \ +! RUN: | FileCheck %s -check-prefix=CHECK-LOONGARCH64 + ! CHECK-A57: "-fc1" "-triple" "aarch64-unknown-linux-gnu" ! CHECK-A57-SAME: "-target-cpu" "cortex-a57" ! CHECK-A57-SAME: "-target-feature" "+v8a" "-target-feature" "+aes" "-target-feature" "+crc" "-target-feature" "+fp-armv8" "-target-feature" "+neon" "-target-feature" "+perfmon" "-target-feature" "+sha2 @@ -86,3 +89,6 @@ ! CHECK-AMDGPU-R600: "-fc1" "-triple" "r600-unknown-unknown" ! CHECK-AMDGPU-R600-SAME: "-target-cpu" "cayman" + +! CHECK-LOONGARCH64: "-fc1" "-triple" "loongarch64-unknown-linux-gnu" +! CHECK-LOONGARCH64-SAME: "-target-cpu" "loongarch64" "-target-feature" "+lsx" "-target-feature" "+64bit" "-target-feature" "+f" "-target-feature" "+d" "-target-feature" "+ual" >From ecf24aee9dc731d3ed72ad7a0fc269ee6271fef6 Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 6 Nov 2024 15:44:35 +0800 Subject: [PATCH 2/2] Modify email address. NFC ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/117108 >From 32e04b6538486006c98c6b805b1057110c3a2c1a Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH 1/5] [Flang] LoongArch64 support for BIND(C) derived types. This patch supports both the passing and returning of BIND(C) type parameters. Reference ABI: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#subroutine-calling-sequence --- flang/lib/Optimizer/CodeGen/Target.cpp| 308 ++ .../Fir/struct-passing-loongarch64-byreg.fir | 232 + ...uct-passing-return-loongarch64-bystack.fir | 80 + .../Fir/struct-return-loongarch64-byreg.fir | 200 4 files changed, 820 insertions(+) create mode 100644 flang/test/Fir/struct-passing-loongarch64-byreg.fir create mode 100644 flang/test/Fir/struct-passing-return-loongarch64-bystack.fir create mode 100644 flang/test/Fir/struct-return-loongarch64-byreg.fir diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp index 9ec055b1aecabb..90ce51552c687f 100644 --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -1081,6 +1081,9 @@ struct TargetLoongArch64 : public GenericTarget { using GenericTarget::GenericTarget; static constexpr int defaultWidth = 64; + static constexpr int GRLen = defaultWidth; /* eight bytes */ + static constexpr int GRLenInChar = GRLen / 8; + static constexpr int FRLen = defaultWidth; /* eight bytes */ CodeGenSpecifics::Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const override { @@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +auto eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + auto subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { + std::size_t numOfEle = vecTy.getLen(); + auto eleTy = vecTy.getEleTy(); + if (!(mlir::isa(eleTy))) { +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + fo
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/117108 >From 32e04b6538486006c98c6b805b1057110c3a2c1a Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH 1/5] [Flang] LoongArch64 support for BIND(C) derived types. This patch supports both the passing and returning of BIND(C) type parameters. Reference ABI: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#subroutine-calling-sequence --- flang/lib/Optimizer/CodeGen/Target.cpp| 308 ++ .../Fir/struct-passing-loongarch64-byreg.fir | 232 + ...uct-passing-return-loongarch64-bystack.fir | 80 + .../Fir/struct-return-loongarch64-byreg.fir | 200 4 files changed, 820 insertions(+) create mode 100644 flang/test/Fir/struct-passing-loongarch64-byreg.fir create mode 100644 flang/test/Fir/struct-passing-return-loongarch64-bystack.fir create mode 100644 flang/test/Fir/struct-return-loongarch64-byreg.fir diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp index 9ec055b1aecabb..90ce51552c687f 100644 --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -1081,6 +1081,9 @@ struct TargetLoongArch64 : public GenericTarget { using GenericTarget::GenericTarget; static constexpr int defaultWidth = 64; + static constexpr int GRLen = defaultWidth; /* eight bytes */ + static constexpr int GRLenInChar = GRLen / 8; + static constexpr int FRLen = defaultWidth; /* eight bytes */ CodeGenSpecifics::Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const override { @@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +auto eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + auto subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { + std::size_t numOfEle = vecTy.getLen(); + auto eleTy = vecTy.getEleTy(); + if (!(mlir::isa(eleTy))) { +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + fo
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
@@ -1151,6 +1154,317 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const unsigned width = + kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + assert(kindMap.getCharacterBitsize(charTy.getFKind()) <= 8 && + "the bit size of characterType as an interoperable type must " + "not exceed 8"); + for (unsigned i = 0; i < charTy.getLen(); ++i) +flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +mlir::Type eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + std::vector subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto &component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +std::vector subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { ylzsx wrote: Thanks for your review. This is my oversight. According to the [LoongArch ABI](https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#vectors-1), 128-bit vector are passed with two GARs with adjacent numbers (if available), and 256-bit vector are passed on the stack. In other cases, the manual has not explicitly specified. I will modify my code to comply with this ABI requirement and report a TODO for other cases. Specifically, * For a vector smaller than 128 bits, report a TODO. * For a vector equal to 128 bits, return `i128` * For a vector larger than 128 bits, pass it on the stack. I will try to make sure it behaves as similar to clang as possible. Looking forward to your suggestions. https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
@@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); ylzsx wrote: It seems that the demo you provided can only be used outside of ISO_C_BINDING. Below is a test I created: ``` program main implicit none character(kind=4) :: i call foo(i) contains subroutine foo(i) bind(c) character(kind=4) :: i end subroutine foo end program main ``` and when I compile it in a X86-64 machine using flang, I got a compilation error: ``` error: Semantic errors in char-struct.f90 ./char-struct.f90:9:30: error: A BIND(C) object must have an interoperable type character(kind=4) :: i ``` When I delete the `bind(c)` attribute, it compile success. However, the argument is passed by reference, and it does not enter functions for architecture-specific processing. >From the results above, it appears that the `character` type used for >Interoperability with C is only one byte, which seems to be the same as the >last line of Table 18.2 in section 18.3.1 of the manual >[18-007r1](https://j3-fortran.org/doc/year/18/18-007r1.pdf#subsection.3437) >I'm still a beginner in Fortran, I'm not sure if there is any >misunderstanding. I would appreciate your suggestions. https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
@@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); ylzsx wrote: Ok, I will add an assertion to document the assumption and address the other feedback as well. Thank you very much for your valuable suggestions! https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/117108 >From 32e04b6538486006c98c6b805b1057110c3a2c1a Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH 1/4] [Flang] LoongArch64 support for BIND(C) derived types. This patch supports both the passing and returning of BIND(C) type parameters. Reference ABI: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#subroutine-calling-sequence --- flang/lib/Optimizer/CodeGen/Target.cpp| 308 ++ .../Fir/struct-passing-loongarch64-byreg.fir | 232 + ...uct-passing-return-loongarch64-bystack.fir | 80 + .../Fir/struct-return-loongarch64-byreg.fir | 200 4 files changed, 820 insertions(+) create mode 100644 flang/test/Fir/struct-passing-loongarch64-byreg.fir create mode 100644 flang/test/Fir/struct-passing-return-loongarch64-bystack.fir create mode 100644 flang/test/Fir/struct-return-loongarch64-byreg.fir diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp index 9ec055b1aecabb..90ce51552c687f 100644 --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -1081,6 +1081,9 @@ struct TargetLoongArch64 : public GenericTarget { using GenericTarget::GenericTarget; static constexpr int defaultWidth = 64; + static constexpr int GRLen = defaultWidth; /* eight bytes */ + static constexpr int GRLenInChar = GRLen / 8; + static constexpr int FRLen = defaultWidth; /* eight bytes */ CodeGenSpecifics::Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const override { @@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +auto eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + auto subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { + std::size_t numOfEle = vecTy.getLen(); + auto eleTy = vecTy.getEleTy(); + if (!(mlir::isa(eleTy))) { +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + fo
[clang] [flang] [Flang][LoongArch] Enable clang command-line options in flang. (PR #118244)
https://github.com/ylzsx created https://github.com/llvm/llvm-project/pull/118244 Mainly including the following LoongArch specific options: -m[no-]lsx, -m[no-]lasx, -msimd=, -m[no-]frecipe, -m[no-]lam-bh, -m[no-]lamcas, -m[no-]ld-seq-sa, -m[no-]div32, -m[no-]annotate-tablejump >From ee9aa4d20d83a43a0a112eff95d6e918c2f62a34 Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 27 Nov 2024 17:48:37 +0800 Subject: [PATCH] [Flang][LoongArch] Enable clang command-line options in flang. Mainly including the following LoongArch specific options: -m[no-]lsx, -m[no-]lasx, -msimd=, -m[no-]frecipe, -m[no-]lam-bh, -m[no-]lamcas, -m[no-]ld-seq-sa, -m[no-]div32, -m[no-]annotate-tablejump --- clang/include/clang/Driver/Options.td | 3 +- clang/lib/Driver/ToolChains/Flang.cpp | 8 + flang/test/Driver/options-loongarch.f90 | 43 + 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 flang/test/Driver/options-loongarch.f90 diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 9c356c9d2ea4ef..7b8e2660192580 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -240,7 +240,8 @@ def m_riscv_Features_Group : OptionGroup<"">, def m_ve_Features_Group : OptionGroup<"">, Group, DocName<"VE">; def m_loongarch_Features_Group : OptionGroup<"">, - Group, DocName<"LoongArch">; + Group, DocName<"LoongArch">, + Visibility<[ClangOption, CLOption, FlangOption]>; def m_libc_Group : OptionGroup<"">, Group, Flags<[HelpHidden]>; diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp index 8c18c88fbde7f7..02323b90b65473 100644 --- a/clang/lib/Driver/ToolChains/Flang.cpp +++ b/clang/lib/Driver/ToolChains/Flang.cpp @@ -213,6 +213,14 @@ void Flang::AddLoongArch64TargetArgs(const ArgList &Args, D.Diag(diag::err_drv_argument_not_allowed_with) << "-mabi" << V; } } + + if (const Arg *A = Args.getLastArg(options::OPT_mannotate_tablejump, + options::OPT_mno_annotate_tablejump)) { +if (A->getOption().matches(options::OPT_mannotate_tablejump)) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back("-loongarch-annotate-tablejump"); +} + } } void Flang::AddPPCTargetArgs(const ArgList &Args, diff --git a/flang/test/Driver/options-loongarch.f90 b/flang/test/Driver/options-loongarch.f90 new file mode 100644 index 00..717bf6385b06d7 --- /dev/null +++ b/flang/test/Driver/options-loongarch.f90 @@ -0,0 +1,43 @@ +!! This test tests options from clang, which are also supported by flang in LoongArch. + +! RUN: %flang -c --target=loongarch64-unknown-linux -mlsx %s -### 2>&1 | FileCheck --check-prefixes=LSX,NOLASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-lsx %s -### 2>&1 | FileCheck --check-prefixes=NOLSX,NOLASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mlasx %s -### 2>&1 | FileCheck --check-prefixes=LSX,LASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-lasx %s -### 2>&1 | FileCheck --check-prefixes=LSX,NOLASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -msimd=none %s -### 2>&1 | FileCheck --check-prefixes=NOLSX,NOLASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -msimd=lsx %s -### 2>&1 | FileCheck --check-prefixes=LSX,NOLASX %s +! RUN: %flang -c --target=loongarch64-unknown-linux -msimd=lasx %s -### 2>&1 | FileCheck --check-prefixes=LSX,LASX %s +! RUN: not %flang -c --target=loongarch64-unknown-linux -msimd=supper %s -### 2>&1 | FileCheck --check-prefix=MSIMD-INVALID %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mfrecipe %s -### 2>&1 | FileCheck --check-prefix=FRECIPE %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-frecipe %s -### 2>&1 | FileCheck --check-prefix=NOFRECIPE %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mlam-bh %s -### 2>&1 | FileCheck --check-prefix=LAMBH %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-lam-bh %s -### 2>&1 | FileCheck --check-prefix=NOLAMBH %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mlamcas %s -### 2>&1 | FileCheck --check-prefix=LAMCAS %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-lamcas %s -### 2>&1 | FileCheck --check-prefix=NOLAMCAS %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mld-seq-sa %s -### 2>&1 | FileCheck --check-prefix=LD-SEQ-SA %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-ld-seq-sa %s -### 2>&1 | FileCheck --check-prefix=NOLD-SEQ-SA %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mdiv32 %s -### 2>&1 | FileCheck --check-prefix=DIV32 %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mno-div32 %s -### 2>&1 | FileCheck --check-prefix=NODIV32 %s +! RUN: %flang -c --target=loongarch64-unknown-linux -mannotat
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types. (PR #117108)
https://github.com/ylzsx edited https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/117108 >From 32e04b6538486006c98c6b805b1057110c3a2c1a Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH 1/3] [Flang] LoongArch64 support for BIND(C) derived types. This patch supports both the passing and returning of BIND(C) type parameters. Reference ABI: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#subroutine-calling-sequence --- flang/lib/Optimizer/CodeGen/Target.cpp| 308 ++ .../Fir/struct-passing-loongarch64-byreg.fir | 232 + ...uct-passing-return-loongarch64-bystack.fir | 80 + .../Fir/struct-return-loongarch64-byreg.fir | 200 4 files changed, 820 insertions(+) create mode 100644 flang/test/Fir/struct-passing-loongarch64-byreg.fir create mode 100644 flang/test/Fir/struct-passing-return-loongarch64-bystack.fir create mode 100644 flang/test/Fir/struct-return-loongarch64-byreg.fir diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp index 9ec055b1aecabb..90ce51552c687f 100644 --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -1081,6 +1081,9 @@ struct TargetLoongArch64 : public GenericTarget { using GenericTarget::GenericTarget; static constexpr int defaultWidth = 64; + static constexpr int GRLen = defaultWidth; /* eight bytes */ + static constexpr int GRLenInChar = GRLen / 8; + static constexpr int FRLen = defaultWidth; /* eight bytes */ CodeGenSpecifics::Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const override { @@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +auto eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + auto subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { + std::size_t numOfEle = vecTy.getLen(); + auto eleTy = vecTy.getEleTy(); + if (!(mlir::isa(eleTy))) { +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + fo
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
ylzsx wrote: Hi, @tblah, Could you take a look and provide your feedback? Thanks in advance! It supports both the passing and returning of BIND(C) type parameters for LoongArch. https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types. (PR #117108)
https://github.com/ylzsx updated https://github.com/llvm/llvm-project/pull/117108 >From 32e04b6538486006c98c6b805b1057110c3a2c1a Mon Sep 17 00:00:00 2001 From: yangzhaoxin Date: Wed, 20 Nov 2024 17:30:43 +0800 Subject: [PATCH 1/2] [Flang] LoongArch64 support for BIND(C) derived types. This patch supports both the passing and returning of BIND(C) type parameters. Reference ABI: https://github.com/loongson/la-abi-specs/blob/release/lapcs.adoc#subroutine-calling-sequence --- flang/lib/Optimizer/CodeGen/Target.cpp| 308 ++ .../Fir/struct-passing-loongarch64-byreg.fir | 232 + ...uct-passing-return-loongarch64-bystack.fir | 80 + .../Fir/struct-return-loongarch64-byreg.fir | 200 4 files changed, 820 insertions(+) create mode 100644 flang/test/Fir/struct-passing-loongarch64-byreg.fir create mode 100644 flang/test/Fir/struct-passing-return-loongarch64-bystack.fir create mode 100644 flang/test/Fir/struct-return-loongarch64-byreg.fir diff --git a/flang/lib/Optimizer/CodeGen/Target.cpp b/flang/lib/Optimizer/CodeGen/Target.cpp index 9ec055b1aecabb..90ce51552c687f 100644 --- a/flang/lib/Optimizer/CodeGen/Target.cpp +++ b/flang/lib/Optimizer/CodeGen/Target.cpp @@ -1081,6 +1081,9 @@ struct TargetLoongArch64 : public GenericTarget { using GenericTarget::GenericTarget; static constexpr int defaultWidth = 64; + static constexpr int GRLen = defaultWidth; /* eight bytes */ + static constexpr int GRLenInChar = GRLen / 8; + static constexpr int FRLen = defaultWidth; /* eight bytes */ CodeGenSpecifics::Marshalling complexArgumentType(mlir::Location loc, mlir::Type eleTy) const override { @@ -1151,6 +1154,311 @@ struct TargetLoongArch64 : public GenericTarget { return GenericTarget::integerArgumentType(loc, argTy); } + + /// Flatten non-basic types, resulting in an array of types containing only + /// `IntegerType` and `FloatType`. + std::vector flattenTypeList(mlir::Location loc, + const mlir::Type type) const { +std::vector flatTypes; + +llvm::TypeSwitch(type) +.template Case([&](mlir::IntegerType intTy) { + if (intTy.getWidth() != 0) +flatTypes.push_back(intTy); +}) +.template Case([&](mlir::FloatType floatTy) { + if (floatTy.getWidth() != 0) +flatTypes.push_back(floatTy); +}) +.template Case([&](mlir::ComplexType cmplx) { + const auto *sem = &floatToSemantics(kindMap, cmplx.getElementType()); + if (sem == &llvm::APFloat::IEEEsingle() || + sem == &llvm::APFloat::IEEEdouble() || + sem == &llvm::APFloat::IEEEquad()) +std::fill_n(std::back_inserter(flatTypes), 2, +cmplx.getElementType()); + else +TODO(loc, "unsupported complx type(not IEEEsingle, IEEEdouble, " + "IEEEquad) as a structure component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::LogicalType logicalTy) { + const auto width = kindMap.getLogicalBitsize(logicalTy.getFKind()); + if (width != 0) +flatTypes.push_back( +mlir::IntegerType::get(type.getContext(), width)); +}) +.template Case([&](fir::CharacterType charTy) { + flatTypes.push_back(mlir::IntegerType::get(type.getContext(), 8)); +}) +.template Case([&](fir::SequenceType seqTy) { + if (!seqTy.hasDynamicExtents()) { +std::size_t numOfEle = seqTy.getConstantArraySize(); +auto eleTy = seqTy.getEleTy(); +if (!mlir::isa(eleTy)) { + auto subTypeList = flattenTypeList(loc, eleTy); + if (subTypeList.size() != 0) +for (std::size_t i = 0; i < numOfEle; ++i) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); +} else { + std::fill_n(std::back_inserter(flatTypes), numOfEle, eleTy); +} + } else +TODO(loc, "unsupported dynamic extent sequence type as a structure " + "component for BIND(C), " + "VALUE derived type argument and type return"); +}) +.template Case([&](fir::RecordType recTy) { + for (auto component : recTy.getTypeList()) { +mlir::Type eleTy = component.second; +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + llvm::copy(subTypeList, std::back_inserter(flatTypes)); + } +}) +.template Case([&](fir::VectorType vecTy) { + std::size_t numOfEle = vecTy.getLen(); + auto eleTy = vecTy.getEleTy(); + if (!(mlir::isa(eleTy))) { +auto subTypeList = flattenTypeList(loc, eleTy); +if (subTypeList.size() != 0) + fo
[clang] [flang] [Flang] LoongArch64 support for BIND(C) derived types in mabi=lp64d. (PR #117108)
https://github.com/ylzsx edited https://github.com/llvm/llvm-project/pull/117108 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [flang] [Flang][LoongArch] Enable clang command-line options in flang. (PR #118244)
@@ -240,7 +240,8 @@ def m_riscv_Features_Group : OptionGroup<"">, def m_ve_Features_Group : OptionGroup<"">, Group, DocName<"VE">; def m_loongarch_Features_Group : OptionGroup<"">, - Group, DocName<"LoongArch">; + Group, DocName<"LoongArch">, + Visibility<[ClangOption, CLOption, FlangOption]>; ylzsx wrote: ``` def m_Group : OptionGroup<"">, Group, DocName<"Target-dependent compilation options">, Visibility<[ClangOption, CLOption]>; ``` I wondor if it inherits from `m_Group` when `Visibility` is not specified. If so, it originally contained `CLOption`. https://github.com/llvm/llvm-project/pull/118244 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits