コグノスケ


link 未来から過去へ表示(*)  link 過去から未来へ表示

link もっと前
2021年4月4日 >>> 2021年4月4日
link もっと後

2021年4月4日

GCCを調べる - GCC 8.3のfoldingバグ - GCC 9.1との比較

目次: GCC

実はGCC 9.1ではcarg, atan2を並べてもエラーが発生しませんので、バグがどこかで直っています。GCC 9.1と8.3の動作の違いを調べることで、原因と直し方がわかるはずです。

cargの変換

GIMPLEを出力しながらコードを追っていくと、下記の関数でcargがatan2に変換され、その後internal compile errorになるようです。

cargの変換箇所

// gcc/gcc/builtins.c

/* Fold a call to builtin carg(a+bi) -> atan2(b,a).  */

static tree
fold_builtin_carg (location_t loc, tree arg, tree type)
{
  if (validate_arg (arg, COMPLEX_TYPE)
      && TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == REAL_TYPE)
    {
      tree atan2_fn = mathfn_built_in (type, BUILT_IN_ATAN2);    //★★この関数がNULL以外を返す条件がある

      if (atan2_fn)
        {
  	  tree new_arg = builtin_save_expr (arg);    //★★ここにくるとcarg → atan2に変換される
	  tree r_arg = fold_build1_loc (loc, REALPART_EXPR, type, new_arg);
	  tree i_arg = fold_build1_loc (loc, IMAGPART_EXPR, type, new_arg);
	  return build_call_expr_loc (loc, atan2_fn, 2, i_arg, r_arg);
	}
    }

  return NULL_TREE;
}

関数mathfn_built_in() はbuiltin_info[uns_fncode].implicit_pがセットされていないとNULLを返す仕組みになっています。

mathfn_built_in() の実装

// gcc/gcc/builtins.c

/* Like mathfn_built_in_1, but always use the implicit array.  */

tree
mathfn_built_in (tree type, combined_fn fn)
{
  return mathfn_built_in_1 (type, fn, /*implicit=*/ 1);    //★★
}

/* Return mathematic function equivalent to FN but operating directly on TYPE,
   if available.  If IMPLICIT_P is true use the implicit builtin declaration,
   otherwise use the explicit declaration.  If we can't do the conversion,
   return null.  */

static tree
mathfn_built_in_1 (tree type, combined_fn fn, bool implicit_p)
{
  built_in_function fcode2 = mathfn_built_in_2 (type, fn);
  if (fcode2 == END_BUILTINS)
    return NULL_TREE;

  if (implicit_p && !builtin_decl_implicit_p (fcode2))    //★★implicit_pフラグがセットされないと変換されない
    return NULL_TREE;

  return builtin_decl_explicit (fcode2);    //★★指定された数学関数のtreeを返す(今回はatan2f)
}


// gcc/gcc/tree.h

/* Return whether the standard builtin function can be used implicitly.  */

static inline bool
builtin_decl_implicit_p (enum built_in_function fncode)
{
  size_t uns_fncode = (size_t)fncode;

  gcc_checking_assert (BUILTIN_VALID_P (fncode));
  return (builtin_info[uns_fncode].decl != NULL_TREE
	  && builtin_info[uns_fncode].implicit_p);    //★★implicit_pフラグがセットされないと変換されない
}


// gcc/gcc/builtins.c

#define CASE_MATHFN(MATHFN) \
  CASE_CFN_##MATHFN: \
  fcode = BUILT_IN_##MATHFN; fcodef = BUILT_IN_##MATHFN##F ; \
  fcodel = BUILT_IN_##MATHFN##L ; break;


/* Return a function equivalent to FN but operating on floating-point
   values of type TYPE, or END_BUILTINS if no such function exists.
   This is purely an operation on function codes; it does not guarantee
   that the target actually has an implementation of the function.  */

static built_in_function
mathfn_built_in_2 (tree type, combined_fn fn)
{
  tree mtype;

...

  switch (fn)
    {
...
    CASE_MATHFN (ATAN)
    CASE_MATHFN (ATAN2)    //★★ここにヒットしてbreak
    CASE_MATHFN (ATANH)
    CASE_MATHFN (CBRT)
... 

    default:
      return END_BUILTINS;
    }

  mtype = TYPE_MAIN_VARIANT (type);
  if (mtype == double_type_node)
    return fcode;
  else if (mtype == float_type_node)
    return fcodef;    //★★ここにくる、返り値はBUILTIN_ATAN2F
  else if (mtype == long_double_type_node)
    return fcodel;
...
  else if (mtype == float128x_type_node)
    return fcodef128x;
  else
    return END_BUILTINS;
}

しかし、cargf → atan2f変換自体はおかしいことではないはずです。

GCC 9.1との比較

GCC 9.1と動作を比較してみます。

GCC 9.1 (OK)GCC 8.3 (NG)
analyze_functions(true) でimplicit_p: false → true analyze_functions(true) でimplicit_p: false → true
pass_lower_cfでcargf → atan2fに置き換え pass_forwpropでcargf → atan2fに置き換え
pass_lower_cfでimplicit_p: true → true pass_forwpropでimplicit_p: true → true
pass_build_ssaでvuseがSSA_NAMEに置き換わる (VAR_DECLのまま)
pass_forwpropでsimplify_builtin_call() pass_forwpropでsimplify_builtin_call() → エラー!!
implicit_p
cargf() をatan2f() に変換するfold_builtin_carg() が発動するかしないかを決めるフラグです。具体的にはbuiltin_info[16].implicit_pです。16はBUILT_IN_ATAN2Fの値です。
simplify_builtin_call()
最初に調べたとおりgimpleのvuseのチェックを行う箇所で、SSA_NAME以外だとinternal compile errorになります。

パスの実行順序は早い順にpass_lower_cf (008t.lower), pass_build_ssa (019t.ssa), pass_forwprop (029t.forwprop1) になります。カッコ内はGCC 9.1でdump-tree-allを指定したときのダンプファイルとの対応です。8.3の場合はpass_forwprop (033t.forwprop) になります。

動作の違いはcargf() → atan2f() の変換が行われるパスです。GCC 9.1はpass_lower_cfですが、GCC 8.3はpass_forwpropです。GCC 9.1は序盤のパスでcargf() → atan2f() の変換が行われるため、pass_build_ssaでvuseが適切に書き換えられて救われるようです。

光明が見えてきました。

編集者:すずき(2023/09/24 11:54)

コメント一覧

  • コメントはありません。
open/close この記事にコメントする



link もっと前
2021年4月4日 >>> 2021年4月4日
link もっと後

管理用メニュー

link 記事を新規作成

<2021>
<<<04>>>
----123
45678910
11121314151617
18192021222324
252627282930-

最近のコメント5件

  • link 24年4月22日
    hdkさん (04/24 08:36)
    「うちのHHFZ4310は15年突破しまし...」
  • link 24年4月22日
    すずきさん (04/24 00:37)
    「ちゃんと数えてないですけど蛍光管が10年...」
  • link 24年4月22日
    hdkさん (04/23 20:52)
    「おお... うちのHHFZ4310より後...」
  • link 20年6月19日
    すずきさん (04/06 22:54)
    「ディレクトリを予め作成しておけば良いです...」
  • link 20年6月19日
    斎藤さん (04/06 16:25)
    「「Preferencesというメニューか...」

最近の記事3件

  • link 24年4月25日
    すずき (04/26 16:49)
    「[AVIFの変換] AVIFが読めないアプリケーションがたまにあるので、AVIF(AV1 Image File Format)...」
  • link 24年2月7日
    すずき (04/24 02:52)
    「[複数の音声ファイルのラウドネスを統一したい] PCやデジタル音楽プレーヤーで音楽を聞いていると、曲によって音量の大小が激しく...」
  • link 24年4月22日
    すずき (04/23 20:13)
    「[仕事部屋の照明が壊れた] いきなり仕事部屋のシーリングライトが消えました。蛍光管の寿命にしては去年(2022年10月19日の...」
link もっとみる

こんてんつ

open/close wiki
open/close Linux JM
open/close Java API

過去の日記

open/close 2002年
open/close 2003年
open/close 2004年
open/close 2005年
open/close 2006年
open/close 2007年
open/close 2008年
open/close 2009年
open/close 2010年
open/close 2011年
open/close 2012年
open/close 2013年
open/close 2014年
open/close 2015年
open/close 2016年
open/close 2017年
open/close 2018年
open/close 2019年
open/close 2020年
open/close 2021年
open/close 2022年
open/close 2023年
open/close 2024年
open/close 過去日記について

その他の情報

open/close アクセス統計
open/close サーバ一覧
open/close サイトの情報

合計:  counter total
本日:  counter today

link About www.katsuster.net
RDFファイル RSS 1.0

最終更新: 04/26 16:49